diff --git a/README.md b/README.md index 3db290f..cd3e466 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,5 @@ - [Task #5](src/neetcode/arrays-&-hashing/top-k-frequent-elements) - _Top K Frequent Elements_ - _[check the task](https://neetcode.io/problems/top-k-elements-in-list)_ - [Task #6](src/neetcode/arrays-&-hashing/encode-and-decode-strings) - _Encode And Decode Strings_ - _[check the task](https://neetcode.io/problems/string-encode-and-decode)_ - [Task #7](src/neetcode/arrays-&-hashing/product-of-array-except-self) - _Products of Array Except Self_ - _[check the task](https://neetcode.io/problems/products-of-array-discluding-self)_ - - [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_ \ No newline at end of file + - [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_ + - [Task #9](src/neetcode/arrays-&-hashing/longest-consecutive-sequence) - _Longest Consecutive Sequence_ - _[check the task](https://neetcode.io/problems/longest-consecutive-sequence)_ \ No newline at end of file diff --git a/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts new file mode 100644 index 0000000..a5bae89 --- /dev/null +++ b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts @@ -0,0 +1,38 @@ +import { longestConsecutive } from './solution'; + +describe('Longest Consecutive Sequence | NeetCode RoadMap | Testcases', () => { + test('#1 Subsequence from start to end', () => { + const nums = [2, 20, 4, 10, 3, 4, 5]; + const expected = 4; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#2 Subsequence unsorted', () => { + const nums = [0, 3, 2, 5, 4, 6, 1, 1]; + const expected = 7; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#3 One element', () => { + const nums = [2]; + const expected = 1; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#4 Empty array', () => { + const nums: number[] = []; + const expected = 0; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#5 Negative nums', () => { + const nums = [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]; + const expected = 7; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); +}); diff --git a/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts new file mode 100644 index 0000000..33e4398 --- /dev/null +++ b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts @@ -0,0 +1,21 @@ +/** + * @param {number[]} nums - an array of integers + * @return {number} - the length of the longest consecutive sequence of elements + * (sequence of elements in which each element is exactly 1 greater than the previous element) that can be formed. + */ +export const longestConsecutive = (nums: number[]): number => { + const unique = new Set(nums); + let mx = 0; + + for (const num of unique) { + if (!unique.has(num - 1)) { + let current = 1; + while (unique.has(num + current)) { + current++; + } + mx = Math.max(mx, current); + } + } + + return mx; +};