Skip to content

Commit 7860ee2

Browse files
committed
feat: JS手写堆
1 parent 82ac0f8 commit 7860ee2

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

JS/algorithm/手写堆.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// https://www.nowcoder.com/practice/f5c52183dfb148489321f881239216c1
2+
// https://github.com/ceilf6/dataStructure/blob/main/learn2code/6-%E6%A0%91/%E4%BA%8C%E5%8F%89%E6%A0%91/%E5%A0%86/%E6%9C%80%E5%B0%8F%E5%A0%86.cpp
3+
4+
// @datastructures-js/priority-queue
5+
6+
const rl = require("readline").createInterface({ input: process.stdin });
7+
var iter = rl[Symbol.asyncIterator]();
8+
const readline = async () => (await iter.next()).value;
9+
10+
void async function () {
11+
const [n, m] = (await readline()).split(' ').map(it => Number(it))
12+
const heap = (await readline()).split(' ').map(it => Number(it))
13+
const ups = (await readline()).split(' ').map(it => Number(it))
14+
function Down(i) {
15+
while (true) { // 不断往下
16+
const lIdx = 2 * i + 1
17+
const rIdx = 2 * i + 2
18+
let curSmall = heap[i]
19+
const l = lIdx < n ? heap[lIdx] : null
20+
const r = rIdx < n ? heap[rIdx] : null
21+
let smallIdx
22+
if (l !== null && l < curSmall) {
23+
smallIdx = 2 * i + 1
24+
curSmall = heap[smallIdx]
25+
}
26+
if (r !== null && r < curSmall) {
27+
smallIdx = 2 * i + 2
28+
}
29+
if (!smallIdx) // 无需交换、无需再往下
30+
break
31+
[heap[i], heap[smallIdx]] = [heap[smallIdx], heap[i]]
32+
if (smallIdx === lIdx)
33+
i = lIdx
34+
else if (smallIdx === rIdx)
35+
i = rIdx
36+
}
37+
}
38+
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
39+
Down(i) // 建堆
40+
}
41+
function Pop() {
42+
const head = heap[0]
43+
heap[0] = heap[heap.length - 1] // 出堆时将最后一个提到头然后往下调整
44+
// heap.splice(heap.length-1,1)
45+
heap.pop()
46+
Down(0)
47+
return head
48+
}
49+
function Up(i) {
50+
while (true) { // 向上调整也需要不断往上
51+
if (i <= 0)
52+
break
53+
const fatherIdx = Math.floor((i - 1) / 2)
54+
const father = heap[fatherIdx]
55+
if (father > heap[i]) {
56+
[heap[i], heap[fatherIdx]] = [father, heap[i]]
57+
i = fatherIdx
58+
} else {
59+
break // 没有交换就无需再往上调整
60+
}
61+
}
62+
}
63+
function Push(newVal) {
64+
heap.push(newVal)
65+
Up(heap.length - 1)
66+
}
67+
let maxNum = Math.max(...heap)
68+
for (const up of ups) {
69+
const head = Pop()
70+
const newVal = head + up
71+
if (newVal > maxNum) // 最大值只可能受新来的威胁
72+
maxNum = newVal
73+
Push(newVal)
74+
// console.log(Math.max(...heap))
75+
console.log(maxNum)
76+
}
77+
}()
78+
79+
/**
80+
*
81+
import heapq
82+
83+
n, m = map(int, input().split())
84+
heap = list(map(int, input().split()))
85+
ups = list(map(int, input().split()))
86+
87+
# 建堆(原地 O(n))
88+
heapq.heapify(heap)
89+
90+
# 维护当前最大值
91+
max_val = max(heap)
92+
93+
for up in ups:
94+
# 取最小值
95+
smallest = heapq.heappop(heap)
96+
97+
new_val = smallest + up
98+
99+
# 放回堆
100+
heapq.heappush(heap, new_val)
101+
102+
# 更新最大值
103+
if new_val > max_val:
104+
max_val = new_val
105+
106+
print(max_val)
107+
*/

0 commit comments

Comments
 (0)