1046. Last Stone Weight Easy

@problem@discussion
#Array#Heap (Priority Queue)



1/**
2 * [1046] Last Stone Weight
3 *
4 * You are given an array of integers stones where stones[i] is the weight of the i^th stone.
5 * We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:
6 * 
7 * 	If x == y, both stones are destroyed, and
8 * 	If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
9 * 
10 * At the end of the game, there is at most one stone left.
11 * Return the weight of the last remaining stone. If there are no stones left, return 0.
12 *  
13 * Example 1:
14 * 
15 * Input: stones = [2,7,4,1,8,1]
16 * Output: 1
17 * Explanation: 
18 * We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
19 * we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
20 * we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
21 * we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
22 * 
23 * Example 2:
24 * 
25 * Input: stones = [1]
26 * Output: 1
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= stones.length <= 30
32 * 	1 <= stones[i] <= 1000
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/last-stone-weight/
38// discuss: https://leetcode.com/problems/last-stone-weight/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_1046() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.