일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Generator
- Python Code
- 운영체제
- Python
- LeetCode
- 715. Range Module
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- Decorator
- Python Implementation
- Regular Expression
- 315. Count of Smaller Numbers After Self
- data science
- t1
- 43. Multiply Strings
- attribute
- 컴퓨터의 구조
- kaggle
- 파이썬
- shiba
- Protocol
- 30. Substring with Concatenation of All Words
- concurrency
- 시바견
- Class
- Substring with Concatenation of All Words
- iterator
- DWG
- Convert Sorted List to Binary Search Tree
- 프로그래머스
Archives
- Today
- Total
Scribbling
LeetCode: 179. Largest Number 본문
프로그래머스에도 있는 문제이다.
문제를 처음 봤을 때, 가장 먼저 nums를 정렬해야된다는 것을 알 것이다.
여기서 정렬을 어떻게 하는지가 핵심인데,
34343 과 3434 중 어느것이 먼저 나와야할지를 생각해보면 된다.
그냥 정렬한다면, 34343이 앞에 와버리는 문제가 있다.
34343+3434
반대의 경우는,
3434+34343
그러나 후자의 경우가 더 크다.
이를 검출할 수 있는 방법은 해당 숫자들을 반복시켜서 얻는 새로운 숫자열을 비교하는 것이다.
3434334343과 34343434 를 비교하면 된다.
아래 코드에서는 숫자를 10번 반복시키는데, 이는 숫자의 최대 길이가 10이기 때문이다. (1과 10000000000의 비교하는 경우)
class Solution:
def largestNumber(self, nums: List[int]) -> str:
nums = list(map(str, nums))
nums.sort(key=lambda x: x*10, reverse=True)
return str(int(''.join(nums)))
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 204. Count Primes (0) | 2021.12.05 |
---|---|
LeetCode: 198. House Robber (0) | 2021.12.04 |
LeetCode: 172. Factorial Trailing Zeroes (0) | 2021.12.04 |
LeetCode: 160. Intersection of Two Linked Lists (0) | 2021.12.02 |
LeetCode: 162. Find Peak Element (0) | 2021.12.01 |