File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Test(unittest.TestCase):
103103

104104
def test_zero_matrix(self):
105105
for [test_matrix, expected] in self.data:
106-
actual = zero_matrix(test_matrix)
106+
actual = zero_matrix1(test_matrix)
107107
self.assertEqual(actual, expected)
108108

109109
if __name__ == "__main__":
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from heapq import heappush, heappop
2+
3+
class Solution:
4+
def findKthLargest(self, nums, k):
5+
min_heap = []
6+
7+
for i in range(k):
8+
heappush(min_heap, nums[i])
9+
10+
for i in range(k, len(nums)):
11+
if nums[i] > min_heap[0]:
12+
heappop(min_heap)
13+
heappush(min_heap, nums[i])
14+
15+
return min_heap[0]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from heapq import heappush, heapreplace
2+
3+
class Solution:
4+
def kClosest(self, points, K):
5+
max_heap = []
6+
for i in range(K):
7+
heappush(max_heap, (-self.distance(points[i]), points[i]))
8+
9+
for i in range(K, len(points)):
10+
distance, _ = max_heap[0]
11+
if -distance > self.distance(points[i]):
12+
heapreplace(max_heap, (-self.distance(points[i]), points[i]))
13+
14+
return [items[1] for items in max_heap]
15+
16+
def distance(self, point):
17+
return (point[0] ** 2) + (point[1] ** 2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import Counter
2+
from heapq import heappush, heappop
3+
4+
class Solution:
5+
def topKFrequent(self, nums, k):
6+
res = []
7+
max_heap = []
8+
nums_freq = Counter(nums)
9+
10+
for num, freq in nums_freq.items():
11+
heappush(max_heap, (-freq, num))
12+
13+
for _ in range(k):
14+
freq, num = heappop(max_heap)
15+
res.append(num)
16+
17+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import Counter
2+
from heapq import heappush, heappop
3+
4+
class Solution:
5+
def frequencySort(self, s: str) -> str:
6+
char_freq = Counter(s)
7+
max_heap = []
8+
res = []
9+
10+
for char, freq in char_freq.items():
11+
heappush(max_heap, (-freq, char))
12+
13+
while max_heap:
14+
item = heappop(max_heap)
15+
freq = -item[0]
16+
char = item[1]
17+
res.append(char * freq)
18+
19+
return ''.join(res)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import heapq
2+
3+
class KthLargest:
4+
5+
def __init__(self, k, nums):
6+
self.heap = []
7+
self.k = k
8+
9+
for num in nums:
10+
self.add(num)
11+
12+
def add(self, val: int) -> int:
13+
heapq.heappush(self.heap, val)
14+
if len(self.heap) > self.k:
15+
heapq.heappop(self.heap)
16+
17+
return self.heap[0]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from heapq import heappush, heappop
2+
3+
class Solution:
4+
def findClosestElements(self, arr, k, x):
5+
x_idx = self.binarySearch(arr, x)
6+
low = max(x_idx - k, 0)
7+
high = min(x_idx + k, len(arr) - 1)
8+
minheap = []
9+
10+
for i in range(low, high+1):
11+
heappush(minheap, (abs(arr[i] - x), arr[i]))
12+
13+
result = []
14+
for _ in range(k):
15+
result.append(heappop(minheap)[1])
16+
17+
result.sort()
18+
return result
19+
20+
21+
def binarySearch(self, arr, x):
22+
l = 0
23+
r = len(arr) - 1
24+
25+
while l < r:
26+
mid = l + (r - l) // 2
27+
28+
if arr[mid] == x:
29+
return mid
30+
elif arr[mid] > x:
31+
r = mid - 1
32+
else:
33+
l = mid + 1
34+
35+
return l

0 commit comments

Comments
 (0)