-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-amount-of-time-to-fill-cups.js
101 lines (87 loc) · 1.82 KB
/
minimum-amount-of-time-to-fill-cups.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
91
92
93
94
95
96
97
98
99
100
101
/**
* 2335. 装满杯子需要的最短总时长
* @param {number[]} amount
* @return {number}
*/
var fillCups = function (amount) {
let count = 0;
// 大顶堆
const maxHeap = new MaxHeap();
for (let i = 0; i < amount.length; i++) {
maxHeap.push(amount[i]);
}
while (true) {
const first = maxHeap.pop();
const second = maxHeap.pop();
if (second === 0) {
count += first;
break;
}
count++;
maxHeap.push(first - 1);
maxHeap.push(second - 1);
}
return count;
};
/**
* 优先队列 - 堆
* 对外暴露接口:
* - push: 向队尾添加元素
* - pop: 从队头取出元素
*/
class MaxHeap {
constructor() {
this.queue = [];
}
push(item) {
this.queue.push(item);
// 上移
this.siftUp(this.size() - 1);
}
pop() {
if (this.size() === 0) {
return;
}
if (this.size() === 1) {
return this.queue.pop();
}
const removedItem = this.queue[0];
this.queue[0] = this.queue.pop();
// 下沉
this.siftDown(0);
return removedItem;
}
// 上移
siftUp(index) {
const parent = index === 0 ? null : (index - 1) >> 1;
if (index > 0 && this.queue[index] > this.queue[parent]) {
swap(this.queue, index, parent);
this.siftUp(parent);
}
}
// 下沉
siftDown(index) {
const left = index * 2 + 1;
const right = index * 2 + 2;
const size = this.size();
let last = index;
if (left < size && this.queue[left] > this.queue[last]) {
last = left;
}
if (right < size && this.queue[right] > this.queue[last]) {
last = right;
}
if (last !== index) {
swap(this.queue, index, last);
this.siftDown(last);
}
}
size() {
return this.queue.length;
}
}
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}