-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestConsecutive.cs
38 lines (31 loc) · 955 Bytes
/
LongestConsecutive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//https://leetcode.com/problems/longest-consecutive-sequence/
namespace LeetCode.Problems;
public sealed class LongestConsecutive : ProblemBase
{
[Theory]
[ClassData(typeof(LongestConsecutive))]
public override void Test(object[] data) => base.Test(data);
protected override void AddTestCases()
=> Add(it => it.ParamArray("[1,0,-1]").Result(3))
.Add(it => it.ParamArray("[100,4,200,1,3,2]").Result(4))
.Add(it => it.ParamArray("[0,3,7,2,5,8,4,6,0,1]").Result(9));
private int Solution(int[] nums)
{
var map = nums.ToHashSet();
var max = 0;
foreach (var num in nums)
{
if (map.Contains(num - 1))
{
continue;
}
var count = 1;
while (map.Contains(num + count))
{
count++;
}
max = Math.Max(max, count);
}
return max;
}
}