-
Notifications
You must be signed in to change notification settings - Fork 0
/
228.py
41 lines (29 loc) · 905 Bytes
/
228.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
"""
Problem:
Given a list of numbers, create an algorithm that arranges them in order to form the
largest possible integer. For example, given [10, 7, 76, 415], you should return
77641510.
"""
from __future__ import annotations
from typing import List
class CustomInt:
def __init__(self, value: int) -> None:
self.value = str(value)
def __lt__(self, other: CustomInt) -> bool:
for c1, c2 in zip(self.value + other.value, other.value + self.value):
if c1 > c2:
return False
elif c1 < c2:
return True
return False
def get_largest(arr: List[int]) -> int:
arr = list(map(CustomInt, arr))
arr.sort(reverse=True)
return int("".join(map(lambda x: x.value, arr)))
if __name__ == "__main__":
print(get_largest([10, 7, 76, 415]))
"""
SPECS:
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(n)
"""