LeetCode: Longest Consecutive Sequence Solution

1/**
2 * @param {number[]} nums
3 * @return {number}
4 */
5var longestConsecutive = function (nums) {
6 const set = new Set(nums)
7 const memo = {}
8
9 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 }
15
16 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

HoningJS

Search Posts