Skip to content

Commit d104b22

Browse files
solves last stone weight
1 parent 62818b9 commit d104b22

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@
280280
| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game) | [![Java](assets/java.png)](src/DivisorGame.java) | |
281281
| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling) | | |
282282
| 1030 | [Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order) | [![Java](assets/java.png)](src/MatrixCellsInDistanceOrder.java) | |
283-
| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive) | [![Java](assets/java.png)](src/ValidBoomerang.java) | |
284-
| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang) | | |
283+
| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive) | | |
284+
| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang) | [![Java](assets/java.png)](src/ValidBoomerang.java) | |
285285
| 1042 | [Flower Planting with no Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent) | | |
286-
| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight) | | |
286+
| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight) | [![Java](assets/java.png)](src/LastStoneWeight.java) | |
287287
| 1047 | [Remove All adjacent Duplicates in String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string) | | |
288288
| 1051 | [Height Checker](https://leetcode.com/problems/height-checker) | | |
289289
| 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number) | | |

src/LastStoneWeight.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Comparator;
2+
import java.util.PriorityQueue;
3+
import java.util.Queue;
4+
5+
public class LastStoneWeight {
6+
public int lastStoneWeight(int[] stones) {
7+
Queue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
8+
for (int stone : stones) maxHeap.add(stone);
9+
int stone1, stone2;
10+
while (maxHeap.size() > 1) {
11+
stone1 = maxHeap.poll();
12+
stone2 = maxHeap.poll();
13+
if (stone1 != stone2) {
14+
maxHeap.add(Math.abs(stone1 - stone2));
15+
}
16+
}
17+
return maxHeap.isEmpty() ? 0 : maxHeap.peek();
18+
}
19+
}

0 commit comments

Comments
 (0)