File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -159,3 +159,4 @@ so that you can use `just tf TEST` command to test.
159
159
| Problem | Difficulty | Solution |
160
160
| - | - | - |
161
161
| 703. Kth Largest Element in a Stream | Easy | [ 703_kth_largest_element_in_a_stream.rs] ( ./tests/703_kth_largest_element_in_a_stream.rs ) |
162
+ | 1046. Last Stone Weight | Easy | [ 1046_last_stone_weight.rs] ( ./tests/1046_last_stone_weight.rs ) |
Original file line number Diff line number Diff line change
1
+ // 1046. Last Stone Weight
2
+ // https://leetcode.com/problems/last-stone-weight/description/
3
+ // Topics: Heap / Priority Queue.
4
+ // Difficulty: Easy.
5
+
6
+ #![ allow( unused) ]
7
+
8
+ #[ test]
9
+ fn test_1046_last_stone_weight ( ) { }
10
+
11
+ #[ derive( Debug ) ]
12
+ pub struct Solution ;
13
+
14
+ // ---------------------------------
15
+ // copy to leetcode starts from here
16
+ // ---------------------------------
17
+
18
+ impl Solution {
19
+ pub fn last_stone_weight ( stones : Vec < i32 > ) -> i32 {
20
+ use std:: collections:: BinaryHeap ;
21
+ let mut heap: BinaryHeap < i32 > = BinaryHeap :: from ( stones) ;
22
+ while heap. len ( ) >= 2 {
23
+ let x = heap. pop ( ) . unwrap ( ) ;
24
+ let y = heap. pop ( ) . unwrap ( ) ;
25
+ let diff = ( x - y) . abs ( ) ;
26
+ if diff != 0 {
27
+ heap. push ( diff) ;
28
+ }
29
+ }
30
+ * heap. peek ( ) . unwrap_or ( & 0 )
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments