-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc119-最小的K个数.js
182 lines (171 loc) · 4.56 KB
/
nc119-最小的K个数.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function GetLeastNumbers_Solution(input, k) {
if(k > input.length) return []
const pq = new PriorityQueue()
for(let e of input) {
pq.push(e)
if(pq.size() > k) pq.pop()
}
const res = []
while(!pq.isEmpty()) res.push(pq.pop())
res.reverse()
return res
}
class PriorityQueue {
constructor(comparator = (a, b) => a > b) {
this.heap = []
this.top = 0
this.comparator = comparator
}
size() {
return this.heap.length
}
isEmpty() {
return this.size() === 0
}
peek() {
return this.heap[this.top]
}
push(...values) {
values.forEach((value) => {
this.heap.push(value)
this.siftUp()
})
return this.size()
}
pop() {
const poppedValue = this.peek()
const bottom = this.size() - 1
if (bottom > this.top) {
this.swap(this.top, bottom)
}
this.heap.pop()
this.siftDown()
return poppedValue
}
replace(value) {
const replacedValue = this.peek()
this.heap[this.top] = value
this.siftDown()
return replacedValue
}
parent = (i) => ((i + 1) >>> 1) - 1
left = (i) => (i << 1) + 1
right = (i) => (i + 1) << 1
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
siftUp = () => {
let node = this.size() - 1
while (node > this.top && this.greater(node, this.parent(node))) {
this.swap(node, this.parent(node))
node = this.parent(node)
}
}
siftDown = () => {
let node = this.top
while (
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
(this.right(node) < this.size() && this.greater(this.right(node), node))
) {
let maxChild =
this.right(node) < this.size() &&
this.greater(this.right(node), this.left(node))
? this.right(node)
: this.left(node)
this.swap(node, maxChild)
node = maxChild
}
}
}
module.exports = {
GetLeastNumbers_Solution : GetLeastNumbers_Solution
};
// another
function GetLeastNumbers_Solution(input, k) {
const arr = []
if(k > input.length) return arr
for(let e of input) {
binaryInsert(arr, e, (a, b) => a - b)
if(arr.length > k) arr.pop()
}
return arr
}
function binaryInsert(array, insertValue, comparator = (a, b) => a - b) {
/*
* These two conditional statements are not required, but will avoid the
* while loop below, potentially speeding up the insert by a decent amount.
* */
if (array.length === 0 || comparator(array[0], insertValue) >= 0) {
array.splice(0, 0, insertValue)
return array;
} else if (array.length > 0 && comparator(array[array.length - 1], insertValue) <= 0) {
array.splice(array.length, 0, insertValue);
return array;
}
let left = 0, right = array.length;
let leftLast = 0, rightLast = right;
while (left < right) {
const inPos = Math.floor((right + left) / 2)
const compared = comparator(array[inPos], insertValue);
if (compared < 0) {
left = inPos;
} else if (compared > 0) {
right = inPos;
} else {
right = inPos;
left = inPos;
}
// nothing has changed, must have found limits. insert between.
if (leftLast === left && rightLast === right) {
break;
}
leftLast = left;
rightLast = right;
}
// use right, because Math.floor is used
array.splice(right, 0, insertValue);
return array
}
module.exports = {
GetLeastNumbers_Solution : GetLeastNumbers_Solution
};
// another
function GetLeastNumbers_Solution(input, k) {
const arr = []
if(k > input.length) return arr
for(let e of input) {
binaryInsert(arr, e, (a, b) => a - b)
if(arr.length > k) arr.pop()
}
return arr
}
function binaryInsert(array, value, compareFn) {
const index = binarySearch(array, value, compareFn);
if (index < 0) {
array.splice(-(index + 1), 0, value);
} else {
array.splice(index, 0, value)
}
return false;
};
function binarySearch(arr, target, compareFn) {
let left = 0; // inclusive
let right = arr.length; // exclusive
let found;
while (left < right) {
const middle = left + ((right - left) >> 1);
const compareResult = compareFn(target, arr[middle]);
if (compareResult > 0) {
left = middle + 1;
} else {
right = middle;
// We are looking for the lowest index so we can't return immediately.
found = !compareResult;
}
}
// left is the index if found, or the insertion point otherwise.
// ~left is a shorthand for -left - 1.
return found ? left : ~left;
};
module.exports = {
GetLeastNumbers_Solution : GetLeastNumbers_Solution
};