-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
52 lines (49 loc) · 1.26 KB
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function lastStoneWeight (stones: number[]): number {
for (let i = getParentIndex(stones.length - 1); i > -1; i--) {
sideUp(stones, i);
}
while (stones.length > 1) {
const a = stones[0];
stones[0] = stones.pop();
sideUp(stones, 0);
const b = stones[0];
if (a === b) {
if (stones.length !== 1) {
stones[0] = stones.pop();
sideUp(stones, 0);
} else {
stones.pop();
}
} else {
stones[0] = a - b;
sideUp(stones, 0);
}
}
if (stones.length === 1) {
return stones[0];
} else {
return 0;
}
}
function sideUp (heap:number[], index:number) {
while (2 * index + 1 < heap.length) {
let child = 2 * index + 1;
if (child + 1 < heap.length && heap[child + 1] > heap[child]) {
child++;
}
if (heap[index] < heap[child]) {
swap(heap, index, child);
index = child;
} else {
break;
}
}
}
function swap (heap:number[], i:number, j:number) {
const tmp = heap[i];
heap[i] = heap[j];
heap[j] = tmp;
}
function getParentIndex (index:number) {
return ((index + 1) >> 1) - 1;
}