728x90
https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/
class Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
count = defaultdict(int)
res = []
for n in nums:
row = count[n]
if len(res) == row:
res.append([])
res[row].append(n)
count[n] += 1
return res
since we don't want any duplicate when creating parital array.
we count the occurence of number. when it becomes more than 1
we create another partital array and append number there
728x90
'Algorithm > NeetCode' 카테고리의 다른 글
Divide Array Into Arrays With Max Difference (0) | 2024.11.24 |
---|---|
Minimum Number of Operations to Make Array Empty (0) | 2024.11.23 |
Design a Food Rating System (0) | 2024.11.21 |
Sum of Absolute Differences in a Sorted Array (0) | 2024.11.20 |
Majority Element II (0) | 2024.11.19 |