Skip to content

Commit 720fd82

Browse files
author
Hamid Gasmi
committed
#239 is completed
1 parent dfc3f22 commit 720fd82

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

09-problems/lc_347_top_k_frequent_element.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import random
2-
import heapq
32

43
class SolutionQuickSelect:
54

65
# Time Complexity: O(n) on average
7-
# Space Complexity: O(1)
6+
# Space Complexity: O(n) due to the dict
87
def top_k_frequent(self, nums: List[int], k: int) -> List[int]:
98

109
occur_dict = dict()
@@ -51,10 +50,12 @@ def _two_partitions(self, unique: List[int], occur_dict, left: int, right: int)
5150

5251
return pivot
5352

53+
import heapq
54+
5455
class SolutionHeap:
5556

56-
# Time Complexity: # O(n + k + (n-k)logk)
57-
# Space Complexity: (O(n + k))
57+
# Time Complexity: O(n + k + (n-k)logk) = O(n + (n-k)logk)
58+
# Space Complexity: O(n + k) = O(n)
5859
def top_k_frequent(self, nums: List[int], k: int) -> List[int]:
5960
# O(n)
6061
occur_dict = dict()

0 commit comments

Comments
 (0)