mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
day 24: 36-valid-sudoku, 128-longest-consecutive-sequence
This commit is contained in:
parent
edf7da5e9f
commit
8acacbbc59
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);
|
||||||
|
};
|
||||||
21
36-valid-sudoku/driver.cpp
Normal file
21
36-valid-sudoku/driver.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<vector<char>> board{
|
||||||
|
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
|
||||||
|
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||||
|
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||||
|
|
||||||
|
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||||
|
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||||
|
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||||
|
|
||||||
|
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||||
|
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||||
|
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
|
||||||
|
bool answer = true;
|
||||||
|
Solution soln;
|
||||||
|
cout << "Validated sudoku correctly? " << soln.test(board, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
57
36-valid-sudoku/soln.cpp
Normal file
57
36-valid-sudoku/soln.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
bool Solution::isValidSudoku(vector<vector<char>> &board)
|
||||||
|
{
|
||||||
|
unordered_map<char, bool> appeared;
|
||||||
|
// check rows
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
appeared.clear();
|
||||||
|
for (int j = 0; j < 9; j++)
|
||||||
|
{
|
||||||
|
char checker = board[i][j];
|
||||||
|
if (checker == '.')
|
||||||
|
continue;
|
||||||
|
if (appeared.find(checker) != appeared.end())
|
||||||
|
return false;
|
||||||
|
appeared[checker] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check columns
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
appeared.clear();
|
||||||
|
for (int j = 0; j < 9; j++)
|
||||||
|
{
|
||||||
|
char checker = board[j][i];
|
||||||
|
if (checker == '.')
|
||||||
|
continue;
|
||||||
|
if (appeared.find(checker) != appeared.end())
|
||||||
|
return false;
|
||||||
|
appeared[checker] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check grids
|
||||||
|
for (int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
appeared.clear();
|
||||||
|
for (int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
for (int k = 0; k < 3; k++)
|
||||||
|
{
|
||||||
|
char checker = board[((i / 3) * 3) + j][((i * 3) % 9) + k];
|
||||||
|
if (checker == '.')
|
||||||
|
continue;
|
||||||
|
if (appeared.find(checker) != appeared.end())
|
||||||
|
return false;
|
||||||
|
appeared[checker] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(vector<vector<char>> &board, bool answer)
|
||||||
|
{
|
||||||
|
return isValidSudoku(board) == answer;
|
||||||
|
}
|
||||||
8
36-valid-sudoku/soln.hpp
Normal file
8
36-valid-sudoku/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool isValidSudoku(vector<vector<char>> &board);
|
||||||
|
bool test(vector<vector<char>> &board, bool answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user