We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 65fd1a5 commit 9b5fb55Copy full SHA for 9b5fb55
last-stone-weight/index.ts
@@ -1,14 +1,14 @@
1
import { PriorityQueue } from "../kth-largest-element-in-a-stream/PriorityQueue.ts";
2
3
export default function lastStoneWeight(stones: number[]): number {
4
- const maxheap = PriorityQueue<number>((a, b) => a - b);
+ const maxheap = PriorityQueue<number>((a, b) =>- a + b);
5
stones.forEach((stone) => maxheap.offer(stone));
6
while (maxheap.length() >= 2) {
7
- const y = maxheap.pop();
8
- const x = maxheap.pop();
+ const y = maxheap.shift();
+ const x = maxheap.shift();
9
if (typeof y === "number" && typeof x === "number" && x !== y) {
10
maxheap.offer(y - x);
11
}
12
13
- return maxheap.tail() ?? 0;
+ return maxheap.head() ?? 0;
14
0 commit comments