From a065570f5fa316b44c9495d828cdee63bff5f6f9 Mon Sep 17 00:00:00 2001 From: "Md. Nazmul Hossain" Date: Mon, 11 Jul 2022 23:01:02 +0600 Subject: [PATCH 1/3] Problem 1: Maximum depth of a binary tree. Done. --- week09/maximum_depth_of_a_binary_tree.cpp | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 week09/maximum_depth_of_a_binary_tree.cpp diff --git a/week09/maximum_depth_of_a_binary_tree.cpp b/week09/maximum_depth_of_a_binary_tree.cpp new file mode 100644 index 00000000..cee96f9e --- /dev/null +++ b/week09/maximum_depth_of_a_binary_tree.cpp @@ -0,0 +1,7 @@ +// Time complexity: O(N), N = Number of nodes. +// Space complexity: O(1) + +int maxDepth(TreeNode* root, int depth = 0){ + if(!root) return depth; + return max(maxDepth(root->left, depth + 1), maxDepth(root->right, depth + 1)); +} From 123edccc88ebe043677671693e7d22939dc7d75a Mon Sep 17 00:00:00 2001 From: "Md. Nazmul Hossain" Date: Wed, 13 Jul 2022 04:18:41 +0600 Subject: [PATCH 2/3] Problem 4: Number of islands. Done. --- week09/number_of_islands.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 week09/number_of_islands.cpp diff --git a/week09/number_of_islands.cpp b/week09/number_of_islands.cpp new file mode 100644 index 00000000..f844b6c7 --- /dev/null +++ b/week09/number_of_islands.cpp @@ -0,0 +1,31 @@ +// Time complexity: O(M * N), M = Number of rows, N = Number of columns of the grid. +// Space complexity: O(1) + +int dr[4] = {-1, 0, 1, 0}; +int dc[4] = {0, 1, 0, -1}; + +void dfs(vector> &grid, int row, int col){ + int r = grid.size(); + int c = grid[0].size(); + if(row == r || col == c || row < 0 || col < 0) return; + if(grid[row][col] == '0' || grid[row][col] == '2') return; + grid[row][col] = '2'; + for(int i = 0; i < 4; i++){ + dfs(grid, row + dr[i], col + dc[i]); + } +} + +int numIslands(vector> &grid){ + int row = grid.size(); + int col = grid[0].size(); + int count = 0; + for(int i = 0; i < row; i++){ + for(int j = 0; j < col; j++){ + if(grid[i][j] == '1'){ + count++; + dfs(grid, i, j); + } + } + } + return count; +} From bf09d43cc7951efc3d281439aacbd686e1cf0ac3 Mon Sep 17 00:00:00 2001 From: "Md. Nazmul Hossain" Date: Thu, 21 Jul 2022 00:17:56 +0600 Subject: [PATCH 3/3] Problem 6: Course schedule. Done. --- week09/course_schedule.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 week09/course_schedule.cpp diff --git a/week09/course_schedule.cpp b/week09/course_schedule.cpp new file mode 100644 index 00000000..edb22b0c --- /dev/null +++ b/week09/course_schedule.cpp @@ -0,0 +1,27 @@ +// Time complexity: O(N), N = Number of courses. +// Space complexity: O(N), N = Number of courses. + +bool hasCycle(vector &col, vector> &graph, int u){ + col[u] = 1; + bool status = false; + for(auto v: graph[u]){ + if(col[v] == 1) status = true; + else if(col[v] == 0){ + status |= hasCycle(col, graph, v); + } + } + col[u] = 2; + return status; +} + +bool canFinish(int numCourses, vector> &prerequisites){ + vector> graph(numCourses); + vector col(numCourses, 0); + for(auto pre: prerequisites){ + graph[pre[1]].push_back(pre[0]); + } + for(int i = 0; i < numCourses; i++){ + if(hasCycle(col, graph, i)) return false; + } + return true; +}