-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
87 lines (75 loc) · 1.75 KB
/
Copy pathtest.js
File metadata and controls
87 lines (75 loc) · 1.75 KB
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
class MinHeap {
constructor(f = (a, b) => a - b) {
this.heap = []
this.compare = f
}
// 일단 마지막에 넣고 올라오기
push(e) {
this.heap.push(e)
let now = this.heap.length - 1
while (now > 0) {
const parent = Math.floor((now - 1) / 2)
if (this.compare(this.heap[parent], this.heap[now]) <= 0) break
;[this.heap[parent], this.heap[now]] = [this.heap[now], this.heap[parent]]
now = parent
}
}
// 일단 마지막거 올리고 내리기
pop() {
if (this.heap.length === 0) return null
if (this.heap.length === 1) return this.heap.pop()
const root = this.heap[0]
this.heap[0] = this.heap.pop()
let now = 0
while (true) {
let left = now * 2 + 1
let right = now * 2 + 2
let next = now
if (
left < this.heap.length &&
this.compare(this.heap[left], this.heap[next]) < 0
) {
next = left
}
if (
right < this.heap.length &&
this.compare(this.heap[right], this.heap[next]) < 0
) {
next = right
}
if (next === now) break
;[this.heap[next], this.heap[now]] = [this.heap[now], this.heap[next]]
now = next
}
return root
}
isEmpty() {
return this.heap.length === 0
}
}
const lower = (arr, target) => {
let start = 0
let end = arr.length
while (start < end) {
let mid = Math.floor((start + end) / 2)
if (arr[mid] >= target) {
end = mid
} else {
start = mid + 1
}
}
return end
}
const upper = (arr, target) => {
let start = 0
let end = arr.length
while (start < end) {
let mid = Math.floor((start + end) / 2)
if (arr[mid] > target) {
end = mid
} else {
start = mid + 1
}
}
return end
}