mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
day 24: 36-valid-sudoku, 128-longest-consecutive-sequence
This commit is contained in:
10
128-longest-consecutive-sequence/driver.cpp
Normal file
10
128-longest-consecutive-sequence/driver.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> nums{100, 4, 200, 1, 3, 2};
|
||||
int answer = 4;
|
||||
Solution soln;
|
||||
cout << "Found longest consecutive sequence correctly? " << soln.test(nums, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
27
128-longest-consecutive-sequence/soln.cpp
Normal file
27
128-longest-consecutive-sequence/soln.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::longestConsecutive(vector<int> &nums)
|
||||
{
|
||||
if (nums.size() < 2)
|
||||
return nums.size();
|
||||
sort(nums.begin(), nums.end());
|
||||
int max_len = 1, cur_max_len = 1;
|
||||
for (int i = 1; i < nums.size(); i++)
|
||||
{
|
||||
if (nums[i] == nums[i - 1])
|
||||
continue;
|
||||
if ((nums[i] == nums[i - 1] + 1))
|
||||
cur_max_len++;
|
||||
else
|
||||
{
|
||||
max_len = max(cur_max_len, max_len);
|
||||
cur_max_len = 1;
|
||||
}
|
||||
}
|
||||
return max(max_len, cur_max_len);
|
||||
}
|
||||
|
||||
bool Solution::test(vector<int> &nums, int answer)
|
||||
{
|
||||
return longestConsecutive(nums) == answer;
|
||||
}
|
||||
8
128-longest-consecutive-sequence/soln.hpp
Normal file
8
128-longest-consecutive-sequence/soln.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
int longestConsecutive(vector<int> &nums);
|
||||
bool test(vector<int> &nums, int answer);
|
||||
};
|
||||
Reference in New Issue
Block a user