-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_hashing.py
More file actions
66 lines (50 loc) Β· 1.69 KB
/
array_hashing.py
File metadata and controls
66 lines (50 loc) Β· 1.69 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import heapq
from collections import Counter
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
visited = {}
for index, value in enumerate(nums):
requires = target - value
if requires in visited:
_min = min(index, visited[requires])
_max = max(index, visited[requires])
return [_min, _max]
visited[value] = index
return None
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hash_map = {}
for val in strs:
sorted_val = "".join(sorted(val))
if sorted_val in hash_map:
hash_map[sorted_val].append(val)
else:
hash_map[sorted_val] = [val]
return list(hash_map.values())
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = {}
for num in nums:
counter[num] = counter.get(num, 0) + 1
sorted_encounter: dict = dict(
sorted(
counter.items(),
key=lambda item: item[1],
reverse=True,
)[:k]
)
return list(sorted_encounter.keys())
def topKFrequentHeap(self, nums: List[int], k: int) -> List[int]:
freq_map = Counter(nums)
heap = []
for key, value in freq_map.items():
heapq.heappush(heap, (value, key))
if len(heap) > k:
heapq.heappop(heap)
res = []
for num in heap:
res.append(num[1])
return res
nums = [3, 0, 1, 0]
k = 1
sol = Solution().topKFrequentHeap(nums=nums, k=k)
print("topKFrequent: ", sol)