Skip to content

Commit ffef458

Browse files
add 1046
1 parent 57cf28e commit ffef458

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ _If you like this project, please leave me a star._ ★
5858
|1055|[Fixed Point](https://leetcode.com/problems/fixed-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1055.java) | |Easy||
5959
|1051|[Height Checker](https://leetcode.com/problems/height-checker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1051.java) | |Easy||
6060
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | |Easy||
61+
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1046.java) | |Easy||
6162
|1043|[Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1043.java) | |Medium|DP|
6263
|1038|[Binary Search Tree to Greater Sum Tree](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1038.java) | |Medium|DFS, tree|
6364
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | |Easy|Math|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* 1046. Last Stone Weight
7+
*
8+
* We have a collection of rocks, each rock has a positive integer weight.
9+
*
10+
* Each turn, we choose the two heaviest rocks and smash them together.
11+
* Suppose the stones have weights x and y with x <= y. The result of this smash is:
12+
*
13+
* If x == y, both stones are totally destroyed;
14+
* If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
15+
* At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
16+
*
17+
* Example 1:
18+
* Input: [2,7,4,1,8,1]
19+
* Output: 1
20+
* Explanation:
21+
* We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
22+
* we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
23+
* we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
24+
* we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
25+
*
26+
* Note:
27+
* 1 <= stones.length <= 30
28+
* 1 <= stones[i] <= 1000
29+
* */
30+
public class _1046 {
31+
public static class Solution1 {
32+
public int lastStoneWeight(int[] stones) {
33+
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
34+
for (int stone : stones) {
35+
heap.offer(stone);
36+
}
37+
while (!heap.isEmpty()) {
38+
if (heap.size() >= 2) {
39+
int one = heap.poll();
40+
int two = heap.poll();
41+
int diff = one - two;
42+
heap.offer(diff);
43+
} else {
44+
return heap.poll();
45+
}
46+
}
47+
return -1;
48+
}
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1046;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _1046Test {
10+
private static _1046.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1046.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1}));
20+
}
21+
}

0 commit comments

Comments
 (0)