diff --git a/141-linked-list-cycle/driver.cpp b/141-linked-list-cycle/driver.cpp new file mode 100644 index 0000000..c7a11a1 --- /dev/null +++ b/141-linked-list-cycle/driver.cpp @@ -0,0 +1,16 @@ +#include "soln.hpp" + +int main() +{ + ListNode n5 = ListNode(5); + ListNode n4 = ListNode(4, &n5); + ListNode n3 = ListNode(3, &n4); + ListNode n2 = ListNode(2, &n3); + ListNode n1 = ListNode(1, &n2); + n5.next = &n3; + bool answer = true; + + Solution soln; + cout << "Checked cycle correctly? " << soln.test(&n1, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/141-linked-list-cycle/soln.cpp b/141-linked-list-cycle/soln.cpp new file mode 100644 index 0000000..6feb703 --- /dev/null +++ b/141-linked-list-cycle/soln.cpp @@ -0,0 +1,19 @@ +#include "soln.hpp" + +bool Solution::hasCycle(ListNode *head) +{ + ListNode *slow = head, *fast = head; + while (fast && fast->next) + { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) + return true; + } + return false; +} + +bool Solution::test(ListNode *head, bool answer) +{ + return hasCycle(head) == answer; +} diff --git a/141-linked-list-cycle/soln.hpp b/141-linked-list-cycle/soln.hpp new file mode 100644 index 0000000..7ca530b --- /dev/null +++ b/141-linked-list-cycle/soln.hpp @@ -0,0 +1,18 @@ +#include +using namespace std; +/* Singly linked list*/ +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution +{ +public: + bool hasCycle(ListNode *head); + bool test(ListNode *head, bool answer); +}; \ No newline at end of file diff --git a/200-number-of-islands/driver.cpp b/200-number-of-islands/driver.cpp new file mode 100644 index 0000000..03a8339 --- /dev/null +++ b/200-number-of-islands/driver.cpp @@ -0,0 +1,13 @@ +#include "soln.hpp" + +int main() +{ + vector> grid{{'1', '1', '1', '1', '0'}, + {'1', '1', '0', '1', '0'}, + {'1', '1', '1', '0', '1'}, + {'0', '0', '0', '1', '1'}}; + int answer = 2; + Solution soln; + cout << "Found no. of islands correctly? " << soln.test(grid, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/200-number-of-islands/soln.cpp b/200-number-of-islands/soln.cpp new file mode 100644 index 0000000..ee2370c --- /dev/null +++ b/200-number-of-islands/soln.cpp @@ -0,0 +1,52 @@ +#include "soln.hpp" + +int Solution::numIslands(vector> &grid) +{ + // queue> current_island{pair{0,0}}; + int islands = 0; + for (int i = 0; i < grid.size(); i++) + { + for (int j = 0; j < grid[0].size(); j++) + { + // found land + if (grid[i][j] == '1') + { + islands++; + grid[i][j] = '0'; + queue> adjs; + adjs.push({i, j}); + while (!adjs.empty()) + { + pair adj = adjs.front(); + adjs.pop(); + if (adj.first + 1 < grid.size() && grid[adj.first + 1][adj.second] == '1') + { + grid[adj.first + 1][adj.second] = '0'; + adjs.push({adj.first + 1, adj.second}); + } + if (adj.first > 0 && grid[adj.first - 1][adj.second] == '1') + { + grid[adj.first - 1][adj.second] = '0'; + adjs.push({adj.first - 1, adj.second}); + } + if (adj.second + 1 < grid[0].size() && grid[adj.first][adj.second + 1] == '1') + { + grid[adj.first][adj.second + 1] = '0'; + adjs.push({adj.first, adj.second + 1}); + } + if (adj.second > 0 && grid[adj.first][adj.second - 1] == '1') + { + grid[adj.first][adj.second - 1] = '0'; + adjs.push({adj.first, adj.second - 1}); + } + } + } + } + } + return islands; +} + +bool Solution::test(vector> &grid, int answer) +{ + return numIslands(grid) == answer; +} diff --git a/200-number-of-islands/soln.hpp b/200-number-of-islands/soln.hpp new file mode 100644 index 0000000..8fa6250 --- /dev/null +++ b/200-number-of-islands/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int numIslands(vector> &grid); + bool test(vector> &grid, int answer); +}; \ No newline at end of file