Skip to content

Commit 9b5fb55

Browse files
committed
Update index.ts
1 parent 65fd1a5 commit 9b5fb55

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

last-stone-weight/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { PriorityQueue } from "../kth-largest-element-in-a-stream/PriorityQueue.ts";
22

33
export default function lastStoneWeight(stones: number[]): number {
4-
const maxheap = PriorityQueue<number>((a, b) => a - b);
4+
const maxheap = PriorityQueue<number>((a, b) =>- a + b);
55
stones.forEach((stone) => maxheap.offer(stone));
66
while (maxheap.length() >= 2) {
7-
const y = maxheap.pop();
8-
const x = maxheap.pop();
7+
const y = maxheap.shift();
8+
const x = maxheap.shift();
99
if (typeof y === "number" && typeof x === "number" && x !== y) {
1010
maxheap.offer(y - x);
1111
}
1212
}
13-
return maxheap.tail() ?? 0;
13+
return maxheap.head() ?? 0;
1414
}

0 commit comments

Comments
 (0)