반응형
Problem
이번 문제는 nums 각 값을 nums의 다른 값과 비교하여 작은값의 갯수를 배열로 return 해주는 문제이다
나의 풀이
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
res = list()
for i in nums:
cnt = 0
for j in nums:
if i > j:
cnt += 1
res.append(cnt)
return res
열심히 짱구를 굴려봤는데 for 두번을 사용하지않고는 문제가 풀리지 않았다.
다른 사람의 풀이
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
l1 = sorted(nums)
return [l1.index(i) for i in nums]
ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 개고수다!
나는 sort로 풀려고했는데 sort해서 index를 체크하면 문제가 풀린다는 것 까지 생각을 안했다.
반응형
'Python > algorithm' 카테고리의 다른 글
[Leetcode] 1528. Shuffle String (1) | 2023.01.12 |
---|---|
[Leetcode] 2236. Root Equals Sum of Children (1) | 2023.01.11 |
[Leetcode] 1281. Subtract the Product and Sum of Digits of an Integer (2) | 2023.01.04 |
[Leetcode] 1431. Kids With the Greatest Number of Candies (1) | 2023.01.02 |
[Leetcode] 2413. Smallest Even Multiple (1) | 2022.12.31 |