Skip to content

Commit dcdf35f

Browse files
authored
Merge pull request #167 from yolophg/main
2 parents ea6af32 + 2cd3f4d commit dcdf35f

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

graph-valid-tree/yolophg.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
class Solution {
5+
validTree(n, edges) {
6+
// initialize Union-Find data structure
7+
const parent = Array(n)
8+
.fill(0)
9+
.map((_, index) => index);
10+
const rank = Array(n).fill(1);
11+
12+
// find function with path compression
13+
function find(x) {
14+
if (parent[x] !== x) {
15+
parent[x] = find(parent[x]);
16+
}
17+
return parent[x];
18+
}
19+
20+
// union function with union by rank
21+
function union(x, y) {
22+
const rootX = find(x);
23+
const rootY = find(y);
24+
if (rootX !== rootY) {
25+
if (rank[rootX] > rank[rootY]) {
26+
parent[rootY] = rootX;
27+
} else if (rank[rootX] < rank[rootY]) {
28+
parent[rootX] = rootY;
29+
} else {
30+
parent[rootY] = rootX;
31+
rank[rootX] += 1;
32+
}
33+
} else {
34+
// if rootX == rootY, there is a cycle
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
// process each edge
41+
for (const [u, v] of edges) {
42+
if (!union(u, v)) {
43+
// if union returns false, a cycle is detected
44+
return false;
45+
}
46+
}
47+
48+
// if all unions are successful, it's a valid tree
49+
return true;
50+
}
51+
}

house-robber-ii/yolophg.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var rob = function (nums) {
5+
// to rob a linear list of houses
6+
function robLinear(houses) {
7+
let prev1 = 0;
8+
let prev2 = 0;
9+
10+
for (let money of houses) {
11+
let temp = Math.max(prev1, money + prev2);
12+
prev2 = prev1;
13+
prev1 = temp;
14+
}
15+
16+
return prev1;
17+
}
18+
19+
// 1. excluding the last house (rob from first to second-to-last)
20+
// 2. excluding the first house (rob from second to last)
21+
let robFirstToSecondLast = robLinear(nums.slice(0, -1));
22+
let robSecondToLast = robLinear(nums.slice(1));
23+
24+
// return the maximum money
25+
return Math.max(robFirstToSecondLast, robSecondToLast);
26+
};

house-robber/yolophg.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var rob = function (nums) {
5+
// to store the maximum money that can be robbed up to each house
6+
let memo = new Array(nums.length).fill(-1);
7+
8+
function robFrom(i) {
9+
// if the index is out of bounds, return 0
10+
if (i >= nums.length) return 0;
11+
12+
// 1. rob this house and move to the house two steps ahead
13+
// 2. skip this house and move to the next house
14+
// take the maximum of these two choices
15+
let result = Math.max(nums[i] + robFrom(i + 2), robFrom(i + 1));
16+
17+
// store the result
18+
memo[i] = result;
19+
20+
return result;
21+
}
22+
23+
// start robbing from the first house
24+
return robFrom(0);
25+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time Complexity: O(n^2)
2+
// Space Complexity: O(1)
3+
4+
var longestPalindrome = function (s) {
5+
let start = 0,
6+
maxLength = 1;
7+
8+
// to expand around the center and update the start and maxLength
9+
function expandAroundCenter(left, right) {
10+
while (left >= 0 && right < s.length && s[left] === s[right]) {
11+
left--;
12+
right++;
13+
}
14+
// update the start and maxLength if a longer palindrome is found
15+
if (maxLength < right - left - 1) {
16+
start = left + 1;
17+
maxLength = right - left - 1;
18+
}
19+
}
20+
21+
// iterate through each character in the string
22+
for (let i = 0; i < s.length; i++) {
23+
// expand around the current character
24+
expandAroundCenter(i, i);
25+
// expand around the current and next character
26+
expandAroundCenter(i, i + 1);
27+
}
28+
29+
// return the longest palindromic substring
30+
return s.substring(start, start + maxLength);
31+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time Complexity: O(n + m) m : number of edges
2+
// Space Complexity: O(n)
3+
4+
class Solution {
5+
countComponents(n, edges) {
6+
// initialize the parent array where each node is its own parent initially
7+
const parent = new Array(n).fill(0).map((_, index) => index);
8+
9+
// to find the root of a node with path compression
10+
const find = (node) => {
11+
if (parent[node] !== node) {
12+
parent[node] = find(parent[node]);
13+
}
14+
return parent[node];
15+
};
16+
17+
// to union two nodes
18+
const union = (node1, node2) => {
19+
const root1 = find(node1);
20+
const root2 = find(node2);
21+
if (root1 !== root2) {
22+
parent[root1] = root2;
23+
}
24+
};
25+
26+
// union all the edges
27+
for (let [a, b] of edges) {
28+
union(a, b);
29+
}
30+
31+
// count the number of unique roots
32+
const uniqueRoots = new Set();
33+
for (let i = 0; i < n; i++) {
34+
uniqueRoots.add(find(i));
35+
}
36+
37+
return uniqueRoots.size;
38+
}
39+
}

0 commit comments

Comments
 (0)