-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
90 lines (84 loc) · 1.89 KB
/
solution.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @param {number[]} apples
* @param {number[]} days
* @return {number}
*/
var eatenApples = function (apples, days) {
const heap = [];
let result = 0;
for (let i = 0; i < apples.length; i++) {
if (apples[i] > 0) {
heap.push([i + days[i], apples[i], ]);
sideUp(heap);
}
while (heap.length && heap[0][0] <= i) {
heapPop(heap);
}
if (heap.length === 0) {
continue;
}
result++;
if (heap[0][1] > 1) {
heap[0][1]--;
} else {
heapPop(heap);
}
}
let day = days.length;
while (heap.length) {
while (heap.length && heap[0][0] <= day) {
heapPop(heap);
}
if (heap.length === 0) {
break;
}
result++;
day++;
if (heap[0][1] > 1) {
heap[0][1]--;
} else {
heapPop(heap);
}
}
return result;
};
function heapPop (heap) {
if (heap.length === 1) {
heap.pop();
return;
}
heap[0] = heap.pop();
sideDown(heap);
}
function swap (list, i, j) {
const tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
function sideUp (heap) {
let index = heap.length - 1;
while (index > 0) {
const parent = ((index + 1) >> 1) - 1;
if (heap[parent][0] > heap[index][0]) {
swap(heap, index, parent);
index = parent;
} else {
break;
}
}
}
function sideDown (heap) {
let index = 0;
while (2 * index + 1 < heap.length) {
let child = 2 * index + 1;
if (child + 1 < heap.length && heap[child + 1][0] < heap[child][0]) {
child++;
}
if (heap[child][0] < heap[index][0]) {
swap(heap, child, index);
index = child;
} else {
break;
}
}
}