LeetCode: Advantage Shuffle Solution
1/**2 * @param {number[]} A3 * @param {number[]} B4 * @return {number[]}5 */6var advantageCount = function (A, B) {7 A = A.sort((a, b) => a - b)8 B = B.map((el, i) => [el, i]).sort(([a], [b]) => b - a) // keep the index for later sort to the start position910 for (const b of B) {11 const a = A.slice(-1)[0]12 if (a > b[0]) {13 b.push(a)14 A.pop()15 }16 }1718 return B.map(([b, i, a]) => [b, i, a != null ? a : A.pop()])19 .sort(([, iA], [, iB]) => iA - iB)20 .map(([, , el]) => el)21}
Comments
Loading comments...
Tags
leetcode
array
greedy
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.