Skip to content

Commit a08ce3c

Browse files
authored
Create 870-advantage-shuffle.js
1 parent adceb56 commit a08ce3c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

870-advantage-shuffle.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} A
3+
* @param {number[]} B
4+
* @return {number[]}
5+
*/
6+
function advantageCount(aArr, B) {
7+
const A = aArr.sort((a, b) => a - b)
8+
const n = A.length
9+
const res = []
10+
let pq = []
11+
for(let i = 0; i < n; i++) {
12+
pq.push([B[i], i])
13+
}
14+
pq.sort((a, b) => b[0] - a[0])
15+
let lo = 0
16+
let hi = n - 1
17+
while(pq.length > 0) {
18+
let cur = pq.shift()
19+
let idx = cur[1]
20+
let val = cur[0]
21+
if (A[hi] > val) {
22+
res[idx] = A[hi--]
23+
} else {
24+
res[idx] = A[lo++]
25+
}
26+
}
27+
return res
28+
}

0 commit comments

Comments
 (0)