LeetCode: Longest Consecutive Sequence Solution
1/**2 * @param {number[]} nums3 * @return {number}4 */5var longestConsecutive = function (nums) {6 const set = new Set(nums)7 const memo = {}89 const recursion = num => {10 if (memo[num] !== undefined) {11 return memo[num]12 }13 return (memo[num] = set.has(num - 1) ? recursion(num - 1) + 1 : 1)14 }1516 return Math.max.apply(null, [...set].map(recursion).concat([0]))17}
Comments
Loading comments...
Tags
leetcode
array
dynamic programming
sorting
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
CSSBattle 1.5: Acid Rain
Jun 8, 2021
Previous Post
CSSBattle 1.3: Push Button
Jun 3, 2021