From 8acacbbc59e15e1332e096c71d99f9f0615aeabd Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Mon, 23 Oct 2023 09:59:07 -0700 Subject: [PATCH] day 24: 36-valid-sudoku, 128-longest-consecutive-sequence --- 128-longest-consecutive-sequence/driver.cpp | 10 ++++ 128-longest-consecutive-sequence/soln.cpp | 27 ++++++++++ 128-longest-consecutive-sequence/soln.hpp | 8 +++ 36-valid-sudoku/driver.cpp | 21 ++++++++ 36-valid-sudoku/soln.cpp | 57 +++++++++++++++++++++ 36-valid-sudoku/soln.hpp | 8 +++ 6 files changed, 131 insertions(+) create mode 100644 128-longest-consecutive-sequence/driver.cpp create mode 100644 128-longest-consecutive-sequence/soln.cpp create mode 100644 128-longest-consecutive-sequence/soln.hpp create mode 100644 36-valid-sudoku/driver.cpp create mode 100644 36-valid-sudoku/soln.cpp create mode 100644 36-valid-sudoku/soln.hpp diff --git a/128-longest-consecutive-sequence/driver.cpp b/128-longest-consecutive-sequence/driver.cpp new file mode 100644 index 0000000..537c1aa --- /dev/null +++ b/128-longest-consecutive-sequence/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector 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; +} \ No newline at end of file diff --git a/128-longest-consecutive-sequence/soln.cpp b/128-longest-consecutive-sequence/soln.cpp new file mode 100644 index 0000000..7b09591 --- /dev/null +++ b/128-longest-consecutive-sequence/soln.cpp @@ -0,0 +1,27 @@ +#include "soln.hpp" + +int Solution::longestConsecutive(vector &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 &nums, int answer) +{ + return longestConsecutive(nums) == answer; +} \ No newline at end of file diff --git a/128-longest-consecutive-sequence/soln.hpp b/128-longest-consecutive-sequence/soln.hpp new file mode 100644 index 0000000..a51d555 --- /dev/null +++ b/128-longest-consecutive-sequence/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int longestConsecutive(vector &nums); + bool test(vector &nums, int answer); +}; \ No newline at end of file diff --git a/36-valid-sudoku/driver.cpp b/36-valid-sudoku/driver.cpp new file mode 100644 index 0000000..239e397 --- /dev/null +++ b/36-valid-sudoku/driver.cpp @@ -0,0 +1,21 @@ +#include "soln.hpp" + +int main() +{ + vector> 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; +} \ No newline at end of file diff --git a/36-valid-sudoku/soln.cpp b/36-valid-sudoku/soln.cpp new file mode 100644 index 0000000..f26aa1f --- /dev/null +++ b/36-valid-sudoku/soln.cpp @@ -0,0 +1,57 @@ +#include "soln.hpp" + +bool Solution::isValidSudoku(vector> &board) +{ + unordered_map 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> &board, bool answer) +{ + return isValidSudoku(board) == answer; +} \ No newline at end of file diff --git a/36-valid-sudoku/soln.hpp b/36-valid-sudoku/soln.hpp new file mode 100644 index 0000000..16afa06 --- /dev/null +++ b/36-valid-sudoku/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + bool isValidSudoku(vector> &board); + bool test(vector> &board, bool answer); +}; \ No newline at end of file