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 2f982d3 commit 694cf23Copy full SHA for 694cf23
โlongest-consecutive-sequence/jangwonyoon.jsโ
@@ -0,0 +1,33 @@
1
+/**
2
+ * ์๊ฐ ๋ณต์ก๋: O(n log n)
3
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
4
+ *
5
+ * @param {number[]} nums
6
+ * @return {number}
7
+ */
8
+var longestConsecutive = function(nums) {
9
+ // ์์ธ
10
+ if (nums.length === 0) {
11
+ return 0;
12
+ }
13
+
14
+ const sortedArr = [...new Set(nums)].sort((a, b) => a - b);
15
16
+ let result = 1;
17
+ let consecutive = 1;
18
19
+ for (let i = 0; i < sortedArr.length - 1; i++) {
20
+ const curr = sortedArr[i];
21
+ const next = sortedArr[i + 1];
22
23
+ if (next === curr + 1) {
24
+ consecutive += 1;
25
+ } else {
26
+ consecutive = 1;
27
28
29
+ result = Math.max(result, consecutive);
30
31
32
+ return result;
33
+};
0 commit comments