Big Sorting, a HackerRank problem

Here is the link to the problem: https://www.hackerrank.com/challenges/big-sorting, and the description copied/pasted:

Consider an array of numeric strings, , where each string is a positive number with anywhere from  to  digits. Sort the array's elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

The numbers will be massive, up to 10^6 digits. One option is to use the BigInteger data type, but operations with BigIntegers are very slow. The other one, which is the one that I had opted to use, uses the Array.Sort() method in C#. The only caveat here is to implement the IComparer.Compare method. In our case it is very easy to implement since the inputs are in string format, with no leading zero. The key part of the code in my opinion is the implementation of this function, the rest is just syntax. Thanks, Marcelo.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections;
class Solution
{
    public class myCompareClass : IComparer
    {
        int IComparer.Compare(Object x, Object y)
        {
            string s1 = (string)x;
            string s2 = (string)y;

            if (s1.Length < s2.Length) return -1;
            if (s1.Length > s2.Length) return 1;

            for (int i = 0; i < s1.Length; i++)
            {
                if (s1[i] < s2[i]) return -1;
                if (s1[i] > s2[i]) return 1;
            }

            return 0;
        }

    }
    static void Main(String[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] unsorted = new string[n];
        for (int unsorted_i = 0; unsorted_i < n; unsorted_i++)
        {
            unsorted[unsorted_i] = Console.ReadLine();
        }

        myCompareClass myCompare = new myCompareClass();
        Array.Sort(unsorted, myCompare);

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(unsorted[i]);
        }
    }
}

Comments

  1. this sounds like a perfect fit for a radix sort, but I couldn't resist a temptation to cheat and use a built-in sort with custom comparator too :)

    #include
    #include
    #include
    #include
    using namespace std;

    int main() {
    int n; cin >> n;
    vector strings(n);
    for (int i = 0; i < n; ++i) cin >> strings[i];
    sort(strings.begin(), strings.end(), [](const string& s1, const string& s2) {
    if (s1.size() < s2.size()) return true;
    if (s1.size() > s2.size()) return false;
    return s1 < s2;
    });
    copy(strings.cbegin(), strings.cend(), ostream_iterator(cout, "\n")); cout << endl;
    return 0;
    }

    http://ideone.com/ScAC6m

    ReplyDelete
    Replies
    1. The inline function is so cool! Good job, buddy!!!

      Delete
    2. yep, it's awesome to see C++ getting modern and maintaining its zero-overhead abstractions principle :)

      Delete

Post a Comment

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

The Power Sum, a recursive problem by HackerRank