LeetCode: 3sum With Multiplicity Solution
Approach
Create a hash table counting occurences of two sum
Implementation
1/**2 * @param {number[]} arr3 * @param {number} target4 * @return {number}5 */6var threeSumMulti = function (arr, target) {7 const N = arr.length8 const MOD = 1e9 + 79 const map = new Map()1011 let res = 01213 for (let i = 0; i < N; i++) {14 res = (res + (map.get(target - arr[i]) || 0)) % MOD1516 for (let j = 0; j < i; j++) {17 const twoSum = arr[i] + arr[j]18 map.set(twoSum, (map.get(twoSum) || 0) + 1)19 }20 }2122 return res23}
Comments
Loading comments...
Tags
leetcode
hash table
two pointers
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.
Next Post
LeetCode: Advantage Shuffle
Mar 25, 2021
Previous Post
LeetCode: Vowel Spellchecker
Mar 23, 2021