LeetCode: Minimum Absolute Sum Difference Solution
1/**2 * @param {number[]} nums13 * @param {number[]} nums24 * @return {number}5 */6var minAbsoluteSumDiff = function (nums1, nums2) {7 const diffArr = nums1.map((num1, i) => Math.abs(num1 - nums2[i]))8 const maxDiff = Math.max(...diffArr)9 const maxDiffIdx = diffArr.findIndex(diff => diff === maxDiff)10 const diffSum = diffArr.reduce((acc, el) => acc + el, 0) - maxDiff1112 let res = Infinity13 for (const num of nums1) {14 res = Math.min(res, diffSum + Math.abs(num - nums2[maxDiffIdx]))15 }1617 return res % (1e9 + 7)18}
Comments
Loading comments...
Tags
leetcode
greedy
binary search
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.