class Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
res = []
for num in nums:
r = 0
while r < len(res):
if num not in res[r]:
break
r += 1
if r == len(res):
res.append([])
res[r].append(num)
return resWhere is the size of the array and is the frequency of the most frequent element in the given array.
class Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
i = 0
while i < len(nums):
j = i
r = 0
while j < len(nums) and nums[i] == nums[j]:
if r == len(res):
res.append([])
res[r].append(nums[i])
r += 1
j += 1
i = j
return resclass Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
count = defaultdict(int)
res = []
for num in nums:
row = count[num]
if len(res) == row:
res.append([])
res[row].append(num)
count[num] += 1
return res