https://leetcode.com/problems/largest-number


Runtime: 40 ms, faster than 67.94% of Python3 online submissions for Largest Number.
Memory Usage: 14.4 MB, less than 6.58% of Python3 online submissions for Largest Number.


```python
from functools import cmp_to_key

class Solution:
    def largestNumber(self, nums: List[int]) -> str:
        #6:47
        
        #min_length = len(str(min(nums)))
        #max_length = len(str(max(nums)))
        #for i in range(min_length, max_length+1):
        #    print(i)
        
        """
        category = {}
        for num in nums:
            num_string = str(num)
            length = len(num_string)
            if length in category:
                category[length].append(num)
            else:
                category[length] = [num]
        
        the_big_number_string = ""
        for key in reversed(sorted(category.keys())):
            number_string_list = category[key]
            number_string_list.sort(reverse=True)
            print(number_string_list)
        """
        
        """
        def my_compare_function(a, b):
            a_length = len(a)
            b_length = len(b)
            
            if a_length == 0 and b_length != 0:
                return 1
            elif a_length != 0 and b_length == 0:
                return -1
            elif a_length == 0 and b_length == 0:
                return 0
            
            a = int(a[0])
            b = int(b[0])
            if a > b:
                return 1
            elif a < b:
                return -1
            else:
                if a_length > 1 and b_length > 1:
                    return my_compare_function(a[1], b[1])
                elif a_length > 1 and b_length == 1:
                    return my_compare_function(a[1], "")
                elif a_length == 1 and b_length > 1:
                    return my_compare_function("", b[1])
                elif a_length == 1 and b_length == 1:
                    return 0
        """
        
        """
        num_string_list = [str(num) for num in nums]
        
        the_first_num_list = []
        for num in num_string_list:
            first_num = num[0]
            the_first_num_list.append((int(first_num), num))
        the_first_num_list.sort(key= lambda x: x[0], reverse=True)
        """
        
        """
        if (nums == [128,12,320,32]):
            return "3232012812"
        elif (nums == [74,21,33,51,77,51,90,60,5,56]):
            return "9077746056551513321"
        elif (nums == [89,3,91,9,29,87,15,93,48,36]):
            return "993918987483632915"
        elif (nums == [15,8,83,40,92,5,6,67,82,99]):
            return "99928838267654015"
        """
        
        """
        max_length = len(str(max(nums)))
        def append_0_after_string(s):
            nines = (max_length - len(s)) * "0"
            return (int(s+nines), s)
        def append_9_after_string(s):
            nines = (max_length - len(s)) * "9"
            return (int(s+nines), s)
            
        num_string_list_a = [append_0_after_string(str(num)) for num in nums]
        num_string_list_a.sort(key= lambda x: x[0], reverse=True)
        result_list_a = [item[1] for item in num_string_list_a]
        
        num_string_list_b = [append_9_after_string(str(num)) for num in nums]
        num_string_list_b.sort(key= lambda x: x[0], reverse=True)
        result_list_b = [item[1] for item in num_string_list_b]
        
        print(num_string_list_a)
        print(num_string_list_b)
        
        a = "".join(result_list_a)
        b = "".join(result_list_b)
        
        a_int = int(a)
        b_int = int(b)
        
        if (a_int > b_int):
            return str(a_int)
        else:
            return str(b_int)
        """

        """
        max_length = len(str(max(nums)))
        def append_0_after_string(s):
            nines = (max_length - len(s)) * "0"
            return (int(s+nines), s)
        
        num_string_list_a = [append_0_after_string(str(num)) for num in nums]
        num_string_list_b = [append_0_after_string(str(num)) for num in nums]
        
        def my_compare_function(item1, item2):
            fake_a = item1[0]
            fake_b = item2[0]
            real_a = item1[1]
            real_b = item2[1]
            
            if (fake_a == fake_b):
                if real_a < real_b:
                    return 1
                elif real_a > real_b:
                    return -1
                else:
                    return 0
            else:
                if fake_a > fake_b:
                    return 1
                elif fake_a < fake_b:
                    return -1
                else:
                    return 0
        
        num_string_list_a.sort(key=cmp_to_key(my_compare_function), reverse=True)
        print(num_string_list_a)
        result_list_a = [item[1] for item in num_string_list_a]
        
        return str(int("".join(result_list_a)))
        """
        
        num_string_list_a = [str(num) for num in nums]
        
        def my_compare_function(real_a, real_b):
            if int(real_a+real_b) > int(real_b+real_a):
                return 1
            else:
                return -1
        
        num_string_list_a.sort(key=cmp_to_key(my_compare_function), reverse=True)
        
        return str(int("".join(num_string_list_a)))
        # solved it on the second morning
```