Skip to content

Commit c00280e

Browse files
authored
Merge pull request DaleStudy#165 from nhistory/week10
[Sehwan] Week10 solution with JavaScript
2 parents f518058 + 90424df commit c00280e

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

graph-valid-tree/nhistory.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
/**
3+
* @param {number} n
4+
* @param {number[][]} edges
5+
* @returns {boolean}
6+
*/
7+
validTree(n, edges) {
8+
// A valid tree must have exactly n - 1 edges
9+
if (edges.length !== n - 1) {
10+
return false;
11+
}
12+
13+
// Initialize the adjacency list
14+
let graph = [];
15+
for (let i = 0; i < n; i++) {
16+
graph.push([]);
17+
}
18+
19+
// Populate the adjacency list with edges
20+
for (let [node, neighbor] of edges) {
21+
graph[node].push(neighbor);
22+
graph[neighbor].push(node);
23+
}
24+
25+
let visited = new Set();
26+
27+
// Depth-First Search (DFS) to explore the graph
28+
function dfs(node) {
29+
visited.add(node);
30+
for (let neighbor of graph[node]) {
31+
if (!visited.has(neighbor)) {
32+
dfs(neighbor);
33+
}
34+
}
35+
}
36+
37+
// Start DFS from node 0
38+
dfs(0);
39+
40+
// Check if all nodes were visited
41+
return visited.size === n;
42+
}
43+
}
44+
45+
// TC: O(n)
46+
// SC: O(n)

house-robber-ii/nhistory.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var rob = function (nums) {
2+
// edge case
3+
if (nums.length === 1) return nums[0];
4+
5+
const dp = (start, end) => {
6+
let prev = 0,
7+
curr = 0;
8+
for (let i = start; i < end; i++) {
9+
let temp = curr;
10+
curr = Math.max(nums[i] + prev, curr);
11+
prev = temp;
12+
}
13+
return curr;
14+
};
15+
16+
return Math.max(dp(0, nums.length - 1), dp(1, nums.length));
17+
};
18+
19+
// TC: O(n)
20+
// SC: O(1)

house-robber/nhistory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var rob = function (nums) {
2+
// dynamic programming
3+
let prev = 0,
4+
curr = 0;
5+
6+
for (let num of nums) {
7+
let temp = curr;
8+
curr = Math.max(num + prev, curr);
9+
prev = temp;
10+
}
11+
12+
return curr;
13+
};
14+
15+
// TC: O(n)
16+
// SC: O(1)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var longestPalindrome = function (s) {
2+
let maxStart = 0,
3+
maxEnd = 0;
4+
5+
for (let i = 0; i < s.length; i++) {
6+
let start = i,
7+
end = i;
8+
while (start >= 0 && end < s.length && s[start] === s[end]) {
9+
if (end - start > maxEnd - maxStart) {
10+
maxStart = start;
11+
maxEnd = end;
12+
}
13+
start--;
14+
end++;
15+
}
16+
17+
(start = i), (end = i + 1);
18+
while (start >= 0 && end < s.length && s[start] === s[end]) {
19+
if (end - start > maxEnd - maxStart) {
20+
maxStart = start;
21+
maxEnd = end;
22+
}
23+
start--;
24+
end++;
25+
}
26+
}
27+
28+
return s.slice(maxStart, maxEnd + 1);
29+
};
30+
31+
// TC: O(n^2)
32+
// SC: O(1)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export class Solution {
2+
/**
3+
* @param n: the number of vertices
4+
* @param edges: the edges of undirected graph
5+
* @return: the number of connected components
6+
*/
7+
countComponents(n, edges) {
8+
// write your code here
9+
const graph = Array.from({ length: n }, () => []);
10+
11+
for (const [node, neighbor] of edges) {
12+
graph[node].push(neighbor);
13+
graph[neighbor].push(node);
14+
}
15+
16+
let visited = new Set();
17+
let count = 0;
18+
19+
const dfs = (node) => {
20+
visited.add(node);
21+
for (const nei of graph[node]) {
22+
if (!visited.has(nei)) dfs(nei);
23+
}
24+
};
25+
26+
for (let node = 0; node < n; node++) {
27+
if (!visited.has(node)) {
28+
count++;
29+
dfs(node);
30+
}
31+
}
32+
return count;
33+
}
34+
}
35+
36+
// n: number of node | e: number of edge
37+
// TC: O(n+e)
38+
// SC: O(n+e)

0 commit comments

Comments
 (0)