We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c1838af commit 044cdcfCopy full SHA for 044cdcf
1 file changed
โlongest-consecutive-sequence/Yu-Won.jsโ
@@ -0,0 +1,27 @@
1
+/**
2
+ * ๋ฌธ์ : https://leetcode.com/problems/longest-consecutive-sequence/description/
3
+ *
4
+ * ์๊ตฌ์ฌํญ:
5
+ * ์ ๋ ฌ๋์ง ์์ nums: number[]๋ฅผ Input ์ผ๋ก ๋ฐ์์ ๋
6
+ * ๊ฐ์ฅ ๊ธด ์ฐ์์ผ๋ก ๋ ๊ฐ์ length ๋ฅผ ๋ฐํํ๋ค.
7
+ * ๋จ O(n) ์ด์ด์ผํ๋ค.
8
9
+ * * */
10
+
11
+const longestConsecutive = (nums) => {
12
+ const set = new Set(nums);
13
+ let count = 0;
14
15
+ for(let num of set) {
16
+ if(!set.has(num-1)) {
17
+ let len = 1;
18
19
+ while (set.has(num+len)) {
20
+ len++;
21
+ }
22
23
+ count = Math.max(count, len);
24
25
26
+ return count;
27
+}
0 commit comments