Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 90776b3

Browse files
committedNov 3, 2023
feat: init k closest points to origin
1 parent 9b8eaf8 commit 90776b3

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed
 

‎alternative/easy/last_stone_weight.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import heapq
33

44
def lastStoneWeight(stones: List[int]) -> int:
5-
maxHeap = []
6-
for stone in stones:
7-
heapq.heappush(maxHeap, -stone)
8-
while len(maxHeap) > 1:
9-
stone1 = -heapq.heappop(maxHeap)
10-
stone2 = -heapq.heappop(maxHeap)
5+
stones = [-stone for stone in stones]
6+
heapq.heapify(stones)
7+
while len(stones) > 1:
8+
stone1 = heapq.heappop(stones)
9+
stone2 = heapq.heappop(stones)
1110
if stone1 != stone2:
12-
heapq.heappush(maxHeap, -(stone1 - stone2))
13-
return -maxHeap[0] if maxHeap else 0
11+
heapq.heappush(stones, stone1 - stone2)
12+
13+
return -stones[0] if stones else 0
1414

1515
assert lastStoneWeight([2,7,4,1,8,1]) == 1
1616

‎src/medium/k_closest_points_to_origin.rs

Whitespace-only changes.

‎src/medium/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod find_the_duplicate_number;
1717
pub mod generate_parentheses;
1818
pub mod group_anagrams;
1919
pub mod implement_trie_prefix_tree;
20+
pub mod k_closest_points_to_origin;
2021
pub mod koko_eating_bananas;
2122
pub mod kth_smallest_element_in_a_bst;
2223
pub mod letter_combinations_of_a_phone_number;
@@ -37,7 +38,6 @@ pub mod subsets;
3738
pub mod subsets_ii;
3839
pub mod three_sum;
3940
pub mod three_sum_closest;
40-
4141
pub mod time_based_key_value_store;
4242
pub mod top_k_frequent_elements;
4343
pub mod two_sum_ii_input_array_is_sorted;

0 commit comments

Comments
 (0)