-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path179.largestNumber.py
105 lines (86 loc) · 1.29 KB
/
179.largestNumber.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.
Since the result may be very large, so you need to return a string instead of an integer.
Example 1:
Input: nums = [10,2]
Output: "210"
Example 2:
Input: nums = [3,30,34,5,9]
Output: "9534330"
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 109
Seen this question in a real interview before?
1/5
Yes
No
Accepted
666.8K
Submissions
1.7M
Acceptance Rate
40.4%
Topics
Array
String
Greedy
Sorting
Companies
0 - 3 months
Google
13
Amazon
10
Meta
6
Microsoft
5
Zoho
5
Bloomberg
3
Myntra
2
0 - 6 months
tcs
3
Accenture
3
Oracle
2
Graviton
2
Works Applications
2
6 months ago
Adobe
8
Apple
7
Salesforce
7
Yahoo
6
Uber
4
Goldman Sachs
3
ServiceNow
"""
from typing import List
from functools import cmp_to_key
class Solution:
def largestNumber(self, nums: List[int]) -> str:
for i, n in enumerate(nums):
nums[i] = str(n)
def compare(x, y):
if x + y > y + x:
return -1
else:
return 1
nums = sorted(nums, key=cmp_to_key(compare))
return str(int("".join(nums)))
# return "".join(map(str, nums))
if __name__ == "__main__":
s = Solution()
print(s.largestNumber([10, 2]))