| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.LargestNumber; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class LargestNumberConcatenationSorting : ILargestNumber |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n * k), where n is the number of numbers and k is the number of digits in the larg |
| | 19 | | /// number |
| | 20 | | /// Space complexity - O(n * k), where n is the number of numbers and k is the number of digits in the largest n |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="nums"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public string LargestNumber(int[] nums) |
| 8 | 25 | | { |
| 32 | 26 | | var numsStrings = nums.Select(n => n.ToString()).ToArray(); |
| | 27 | |
|
| 35 | 28 | | Array.Sort(numsStrings, (a, b) => string.Compare(b + a, a + b, StringComparison.Ordinal)); |
| | 29 | |
|
| 8 | 30 | | return numsStrings[0] == "0" ? "0" : string.Join("", numsStrings); |
| 8 | 31 | | } |
| | 32 | | } |