Sort Array by Increasing Frequency - Leetcode 1636 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
6:07 - Coding Explanation

leetcode 1636

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

I use Java and it always amazes me how much shorter and less meandering Python is.

axilie
Автор

You could also solve it using double counting sort, O(N), O(100) space and time complexity.

deadlyecho
Автор

I did it using a counter and a heap. I stored (count, -1 * n) in heap, so it kept both the orderings

SachinRajput
Автор

initially sorting the array in desc order to deal with the desc order asked in ques for same freq is what i used

fiascogamingchannel
Автор

I use min heap + two hashmaps. and you write it in 3 lines?

hashmap = {}
for v in nums:
hashmap[v] = hashmap.get(v, 0) + 1

count = {}
for k, v in hashmap.items():
count.setdefault(v, []).append(k)

heap = []
for k, v in count.items():
heap.append((k, v))

heapq.heapify(heap)
rs = []
while heap:
curr = heapq.heappop(heap)
tmp = sorted((curr[1] * curr[0]), reverse = True)
rs = rs + tmp

return rs

sidazhong