Skip to content

Commit c299218

Browse files
committed
[week1] Longest-consecutive-sequence - gmlwls96
1 parent 912848a commit c299218

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode_study
2+
3+
class Solution {
4+
fun longestConsecutive(nums: IntArray): Int {
5+
nums.sort()
6+
val consecutiveArray = IntArray(nums.size)
7+
var maxCount = 0
8+
for (i in nums.lastIndex - 1 downTo (0)) {
9+
if (nums[i] + 1 == nums[i + 1]) {
10+
consecutiveArray[i] += consecutiveArray[i + 1] + 1
11+
if (consecutiveArray[i] > maxCount) {
12+
maxCount = consecutiveArray[i]
13+
}
14+
}
15+
}
16+
return maxCount + 1
17+
}
18+
}

0 commit comments

Comments
 (0)