Skip to content

Commit 6c849c7

Browse files
committed
ci: fix ci
1 parent dcc5277 commit 6c849c7

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
class Solution:
22

3-
# Time: O(?)
4-
# Space: O(?)
3+
# Time: O(n)
4+
# Space: O(1)
55
def missing_number(self, nums: list[int]) -> int:
6-
# TODO: Implement missing_number
7-
return 0
6+
"""
7+
Find the missing number in an array containing n distinct numbers
8+
in the range [0, n].
9+
10+
Approach: Use the mathematical formula for sum of consecutive integers.
11+
The sum of numbers from 0 to n is n*(n+1)/2.
12+
The missing number = expected_sum - actual_sum.
13+
"""
14+
n = len(nums)
15+
expected_sum = n * (n + 1) // 2
16+
actual_sum = sum(nums)
17+
return expected_sum - actual_sum

leetcode/top_k_frequent_elements/solution.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ def top_k_frequent(self, nums: List[int], k: int) -> List[int]:
1313
"""
1414
counter = Counter(nums)
1515

16-
# Use min heap of size k
16+
# Use min heap of size k - keep the k most frequent elements
1717
heap: list[tuple[int, int]] = []
1818
for num, count in counter.items():
19-
heapq.heappush(heap, (count, num))
20-
if len(heap) > k:
21-
heapq.heappop(heap)
19+
if len(heap) < k:
20+
heapq.heappush(heap, (count, num))
21+
elif count > heap[0][0]:
22+
heapq.heapreplace(heap, (count, num))
2223

24+
# Extract numbers from heap (order doesn't matter for this problem)
2325
return [num for _, num in heap]

0 commit comments

Comments
 (0)