From 2c1e8b5d327252cb7e024e290c5c15e3a7165af8 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Tue, 2 Jul 2024 16:46:48 -0300 Subject: [PATCH 1/5] Added graphValidTree solution --- graph-valid-tree/nhistory.js | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 graph-valid-tree/nhistory.js diff --git a/graph-valid-tree/nhistory.js b/graph-valid-tree/nhistory.js new file mode 100644 index 000000000..c4db3d99d --- /dev/null +++ b/graph-valid-tree/nhistory.js @@ -0,0 +1,46 @@ +class Solution { + /** + * @param {number} n + * @param {number[][]} edges + * @returns {boolean} + */ + validTree(n, edges) { + // A valid tree must have exactly n - 1 edges + if (edges.length !== n - 1) { + return false; + } + + // Initialize the adjacency list + let graph = []; + for (let i = 0; i < n; i++) { + graph.push([]); + } + + // Populate the adjacency list with edges + for (let [node, neighbor] of edges) { + graph[node].push(neighbor); + graph[neighbor].push(node); + } + + let visited = new Set(); + + // Depth-First Search (DFS) to explore the graph + function dfs(node) { + visited.add(node); + for (let neighbor of graph[node]) { + if (!visited.has(neighbor)) { + dfs(neighbor); + } + } + } + + // Start DFS from node 0 + dfs(0); + + // Check if all nodes were visited + return visited.size === n; + } +} + +// TC: O(n) +// SC: O(n) From c31889e1ddc6aa14a2054166fea56177d7c80cd9 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Thu, 4 Jul 2024 23:20:56 -0300 Subject: [PATCH 2/5] Added countComponents solution --- .../nhistory.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 number-of-connected-components-in-an-undirected-graph/nhistory.js diff --git a/number-of-connected-components-in-an-undirected-graph/nhistory.js b/number-of-connected-components-in-an-undirected-graph/nhistory.js new file mode 100644 index 000000000..9587a1930 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/nhistory.js @@ -0,0 +1,38 @@ +export class Solution { + /** + * @param n: the number of vertices + * @param edges: the edges of undirected graph + * @return: the number of connected components + */ + countComponents(n, edges) { + // write your code here + const graph = Array.from({ length: n }, () => []); + + for (const [node, neighbor] of edges) { + graph[node].push(neighbor); + graph[neighbor].push(node); + } + + let visited = new Set(); + let count = 0; + + const dfs = (node) => { + visited.add(node); + for (const nei of graph[node]) { + if (!visited.has(nei)) dfs(nei); + } + }; + + for (let node = 0; node < n; node++) { + if (!visited.has(node)) { + count++; + dfs(node); + } + } + return count; + } +} + +// n: number of node | e: number of edge +// TC: O(n+e) +// SC: O(n+e) From 0d720e4ea993b2eaa9fed7c3b884c28f468487cc Mon Sep 17 00:00:00 2001 From: Sehwan Date: Fri, 5 Jul 2024 16:17:22 -0300 Subject: [PATCH 3/5] Added houseRobber solution --- house-robber/nhistory.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 house-robber/nhistory.js diff --git a/house-robber/nhistory.js b/house-robber/nhistory.js new file mode 100644 index 000000000..f6c31a613 --- /dev/null +++ b/house-robber/nhistory.js @@ -0,0 +1,16 @@ +var rob = function (nums) { + // dynamic programming + let prev = 0, + curr = 0; + + for (let num of nums) { + let temp = curr; + curr = Math.max(num + prev, curr); + prev = temp; + } + + return curr; +}; + +// TC: O(n) +// SC: O(1) From 93c716dbddab26997655c11b3f5046119203707f Mon Sep 17 00:00:00 2001 From: Sehwan Date: Fri, 5 Jul 2024 17:12:11 -0300 Subject: [PATCH 4/5] Added houseRobber2 solution --- house-robber-ii/nhistory.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber-ii/nhistory.js diff --git a/house-robber-ii/nhistory.js b/house-robber-ii/nhistory.js new file mode 100644 index 000000000..dc2d1bf80 --- /dev/null +++ b/house-robber-ii/nhistory.js @@ -0,0 +1,20 @@ +var rob = function (nums) { + // edge case + if (nums.length === 1) return nums[0]; + + const dp = (start, end) => { + let prev = 0, + curr = 0; + for (let i = start; i < end; i++) { + let temp = curr; + curr = Math.max(nums[i] + prev, curr); + prev = temp; + } + return curr; + }; + + return Math.max(dp(0, nums.length - 1), dp(1, nums.length)); +}; + +// TC: O(n) +// SC: O(1) From 90424df553af247d260081ae514a16a7df432919 Mon Sep 17 00:00:00 2001 From: Sehwan Date: Sun, 7 Jul 2024 21:44:14 -0300 Subject: [PATCH 5/5] Added longestPalindromic solution --- longest-palindromic-substring/nhistory.js | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 longest-palindromic-substring/nhistory.js diff --git a/longest-palindromic-substring/nhistory.js b/longest-palindromic-substring/nhistory.js new file mode 100644 index 000000000..6ede35afc --- /dev/null +++ b/longest-palindromic-substring/nhistory.js @@ -0,0 +1,32 @@ +var longestPalindrome = function (s) { + let maxStart = 0, + maxEnd = 0; + + for (let i = 0; i < s.length; i++) { + let start = i, + end = i; + while (start >= 0 && end < s.length && s[start] === s[end]) { + if (end - start > maxEnd - maxStart) { + maxStart = start; + maxEnd = end; + } + start--; + end++; + } + + (start = i), (end = i + 1); + while (start >= 0 && end < s.length && s[start] === s[end]) { + if (end - start > maxEnd - maxStart) { + maxStart = start; + maxEnd = end; + } + start--; + end++; + } + } + + return s.slice(maxStart, maxEnd + 1); +}; + +// TC: O(n^2) +// SC: O(1)