-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
72 lines (63 loc) · 2.69 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Problem: https://www.hackerrank.com/challenges/big-sorting/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
//I was planning to leverage .Net framework's in-built array sorting (which internally uses stable quick sort algorithm) but one test case was still resulting in time-out
// possibly because conversion of big strings present into integer numbers and then their integer based comparison is very intensive operation.
// So I decided to use custom comparer to go for strings based sorting instead.
1. Call Array.Sort (with custom comparer) on the input array containing all the strings.
2. Print all the sorted array elements on console separated by space.
Implementation of Compare method in custom comparer:
1. Let the input strings to be compared are x and y.
2. If length of string x is less than length of string y then return -1.
3. If length of string x is more than length of string y then return 1.
4. If string x and y are exactly same to same same then return 0.
5. If string x and y are not same but have equal length then initialize a counter c to 0 and start a loop:
5.1 if x[c] and y[c] are same then increment c by 1.
5.2 if x[c] and y[c] are not equal then break the loop.
6. Let the digit present at index c of x and y string be dx and dy respectively.
6.1. If digit dx is less than dy then return -1.
6.2. If digit dx is more than dy then return 1.
Time Complexity: O(n Log(n)) // .Net framework class library implementation of Array.Sort method uses stable quick sort
which has n log(n) efficiency.
Space Complexity: O(n)
*/
using System;
using System.Collections.Generic;
class Solution
{
static void Main(String[] args)
{
var n = int.Parse(Console.ReadLine());
var arr = new string[n];
for (var arr_i = 0; arr_i < n; arr_i++)
arr[arr_i] = Console.ReadLine();
Array.Sort(arr, new CustomComparer());
Console.WriteLine(String.Join("\n", arr));
}
}
public class CustomComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x.Length < y.Length)
return -1;
else if (x.Length > y.Length)
{
return 1;
}
else
{
if (x == y)
return 0;
else
{
var i = 0;
while (x[i] == y[i]) i++;
return x[i].CompareTo(y[i]);
}
}
}
}