From 26069de3c87ac5f9bb324382350a11c9aa2a84ae Mon Sep 17 00:00:00 2001 From: Sehwan Date: Mon, 24 Jun 2024 22:00:09 -0300 Subject: [PATCH 1/8] Add designAddAndSearchWords solution --- .../nhistory.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 design-add-and-search-words-data-structure/nhistory.js diff --git a/design-add-and-search-words-data-structure/nhistory.js b/design-add-and-search-words-data-structure/nhistory.js new file mode 100644 index 000000000..2953050eb --- /dev/null +++ b/design-add-and-search-words-data-structure/nhistory.js @@ -0,0 +1,37 @@ +var WordDictionary = function () { + this.dictionary = new Set(); +}; + +/** + * @param {string} word + * @return {void} + */ +WordDictionary.prototype.addWord = function (word) { + this.dictionary.add(word); +}; + +/** + * @param {string} word + * @return {boolean} + */ +WordDictionary.prototype.search = function (word) { + if (word.indexOf(".") != -1) { + // Case of word has a '.' + for (let str of this.dictionary) { + if (str.length != word.length) continue; + let i; + for (i = 0; i < word.length; i++) { + if (word[i] === ".") continue; + if (word[i] != str[i]) break; + } + if (i === str.length) return true; + } + return false; + } else { + return this.dictionary.has(word); + } +}; + +// n: number of node | m: length of the word +// TC: O(n*m) +// SC: O(n*m) From 6b6b88062bea9836e3f9d909e87e2cfe2160d26f Mon Sep 17 00:00:00 2001 From: Sehwan Date: Tue, 25 Jun 2024 10:35:53 -0300 Subject: [PATCH 2/8] Added numIslands solution --- number-of-islands/nhistory.js | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 number-of-islands/nhistory.js diff --git a/number-of-islands/nhistory.js b/number-of-islands/nhistory.js new file mode 100644 index 000000000..694dff026 --- /dev/null +++ b/number-of-islands/nhistory.js @@ -0,0 +1,42 @@ +var numIslands = function (grid) { + // Declare row and column length + const m = grid.length, + n = grid[0].length; + let numIslands = 0; + + // Available directions for depth-first search + const dir = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + + // Function to depth-first search inside of grid + const dfs = (i, j) => { + grid[i][j] = "2"; + + let x, y; + for (d of dir) { + x = i + d[0]; + y = j + d[1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === "1") { + dfs(x, y); + } + } + return; + }; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === "1") { + dfs(i, j); + numIslands++; + } + } + } + return numIslands; +}; + +// TC: O(m*n) +// SC: O(m*n) From 0e134adef7a46fec07bc4c6c726e6edc3dd6cc3c Mon Sep 17 00:00:00 2001 From: nhistory Date: Tue, 25 Jun 2024 21:57:41 -0300 Subject: [PATCH 3/8] Update design-add-and-search-words-data-structure/nhistory.js Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- design-add-and-search-words-data-structure/nhistory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/nhistory.js b/design-add-and-search-words-data-structure/nhistory.js index 2953050eb..bdcfd2410 100644 --- a/design-add-and-search-words-data-structure/nhistory.js +++ b/design-add-and-search-words-data-structure/nhistory.js @@ -32,6 +32,6 @@ WordDictionary.prototype.search = function (word) { } }; -// n: number of node | m: length of the word +// n: number of words | m: length of the word // TC: O(n*m) // SC: O(n*m) From 2dde22e72743baa2d12756e1a63d7aad08fdf780 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Wed, 26 Jun 2024 17:26:38 -0300 Subject: [PATCH 4/8] Added cloneGraph solution --- clone-graph/nhistory.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 clone-graph/nhistory.js diff --git a/clone-graph/nhistory.js b/clone-graph/nhistory.js new file mode 100644 index 000000000..1cafc5dc4 --- /dev/null +++ b/clone-graph/nhistory.js @@ -0,0 +1,33 @@ +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + let visited = {}; + + const dfs = (node) => { + if (!node) return node; + if (visited[node.val]) return visited[node.val]; + + let root = new Node(node.val); + visited[node.val] = root; + + for (let neighbor of node.neighbors) { + root.neighbors.push(dfs(neighbor)); + } + return root; + }; + + return dfs(node); +}; + +// TC: O(n+e) -> n: number of nodes | e: number of edges +// SC: O(v) -> v: length of visited object From b23beafe08e2ca98302bf738f686f537267be264 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Wed, 26 Jun 2024 17:27:27 -0300 Subject: [PATCH 5/8] Revert "Added cloneGraph solution" This reverts commit 11b6a6efe095a5fe27ffe721c26bf8d88c61d64a. --- clone-graph/nhistory.js | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 clone-graph/nhistory.js diff --git a/clone-graph/nhistory.js b/clone-graph/nhistory.js deleted file mode 100644 index 1cafc5dc4..000000000 --- a/clone-graph/nhistory.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * // Definition for a _Node. - * function _Node(val, neighbors) { - * this.val = val === undefined ? 0 : val; - * this.neighbors = neighbors === undefined ? [] : neighbors; - * }; - */ - -/** - * @param {_Node} node - * @return {_Node} - */ -var cloneGraph = function (node) { - let visited = {}; - - const dfs = (node) => { - if (!node) return node; - if (visited[node.val]) return visited[node.val]; - - let root = new Node(node.val); - visited[node.val] = root; - - for (let neighbor of node.neighbors) { - root.neighbors.push(dfs(neighbor)); - } - return root; - }; - - return dfs(node); -}; - -// TC: O(n+e) -> n: number of nodes | e: number of edges -// SC: O(v) -> v: length of visited object From 9df4c39ba65d9fa53c4d3c969625e2663b6e14a1 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Wed, 26 Jun 2024 17:44:11 -0300 Subject: [PATCH 6/8] Added cloneGraph solution --- clone-graph/nhistory.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 clone-graph/nhistory.js diff --git a/clone-graph/nhistory.js b/clone-graph/nhistory.js new file mode 100644 index 000000000..1cafc5dc4 --- /dev/null +++ b/clone-graph/nhistory.js @@ -0,0 +1,33 @@ +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + let visited = {}; + + const dfs = (node) => { + if (!node) return node; + if (visited[node.val]) return visited[node.val]; + + let root = new Node(node.val); + visited[node.val] = root; + + for (let neighbor of node.neighbors) { + root.neighbors.push(dfs(neighbor)); + } + return root; + }; + + return dfs(node); +}; + +// TC: O(n+e) -> n: number of nodes | e: number of edges +// SC: O(v) -> v: length of visited object From 683895312664823192772a1264e7089a34d946e2 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Thu, 27 Jun 2024 14:25:44 -0300 Subject: [PATCH 7/8] Added pacificAtlanticWaterFlow solution --- pacific-atlantic-water-flow/nhistory.js | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pacific-atlantic-water-flow/nhistory.js diff --git a/pacific-atlantic-water-flow/nhistory.js b/pacific-atlantic-water-flow/nhistory.js new file mode 100644 index 000000000..2b6b99d07 --- /dev/null +++ b/pacific-atlantic-water-flow/nhistory.js @@ -0,0 +1,63 @@ +/** + * @param {number[][]} heights + * @return {number[][]} + */ +var pacificAtlantic = function (heights) { + const m = heights.length, + n = heights[0].length; + let result = []; + + const pacific = new Array(m).fill(null).map(() => new Array(n).fill(false)); + const atlantic = new Array(m).fill(null).map(() => new Array(n).fill(false)); + + const dir = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + + const dfs = (i, j, ocean) => { + // Check visited cell + ocean[i][j] = true; + + for (d of dir) { + let x = i + d[0], + y = j + d[1]; + if ( + x >= 0 && + x < m && + y >= 0 && + y < n && + !ocean[x][y] && + heights[x][y] >= heights[i][j] + ) { + dfs(x, y, ocean); + } + } + }; + + // Check the cells can flow left and right edge + for (let i = 0; i < m; i++) { + dfs(i, 0, pacific); + dfs(i, n - 1, atlantic); + } + + // Check the cells can flow top and bottom edge + for (let j = 0; j < n; j++) { + dfs(0, j, pacific); + dfs(m - 1, j, atlantic); + } + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (pacific[i][j] && atlantic[i][j]) { + result.push([i, j]); + } + } + } + return result; +}; + +// TC: O(m*n) +// SC: O(m*n) From ad131abe16871e4f1daaed4f704c766f2afc8644 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Fri, 28 Jun 2024 18:58:28 -0300 Subject: [PATCH 8/8] Added courseSchedule solution --- course-schedule/nhistory.js | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 course-schedule/nhistory.js diff --git a/course-schedule/nhistory.js b/course-schedule/nhistory.js new file mode 100644 index 000000000..0acf8f3f4 --- /dev/null +++ b/course-schedule/nhistory.js @@ -0,0 +1,49 @@ +/** + * @param {number} numCourses + * @param {number[][]} prerequisites + * @return {boolean} + */ +var canFinish = function (numCourses, prerequisites) { + // Initialize in-degree array and graph map + const inDegree = Array(numCourses).fill(0); + const graph = new Map(); + + // Build the graph and compute in-degrees + prerequisites.forEach(([course, pre]) => { + inDegree[course]++; + if (!graph.has(pre)) { + graph.set(pre, []); + } + graph.get(pre).push(course); + }); + + // Queue for courses with no prerequisites + const queue = []; + for (let i = 0; i < numCourses; i++) { + if (inDegree[i] === 0) { + queue.push(i); + } + } + + // Process the courses + let count = 0; + while (queue.length > 0) { + const course = queue.shift(); + count++; + if (graph.has(course)) { + graph.get(course).forEach((nextCourse) => { + inDegree[nextCourse]--; + if (inDegree[nextCourse] === 0) { + queue.push(nextCourse); + } + }); + } + } + + // Return true if all courses can be finished + return count === numCourses; +}; + +// TC: O(V + E) +// V is the number of courses, E is the number of prerequisites. +// SC: O(V + E)