Skip to content

Commit 30dd3b7

Browse files
authored
Merge pull request #152 from yolophg/week-9
2 parents 5c40967 + d2fd111 commit 30dd3b7

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed

clone-graph/yolophg.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(N + E) N = Node + E = Edge
2+
// Space Complexity: O(N + E)
3+
4+
var cloneGraph = function (node) {
5+
// to keep track of all the nodes that have already been copied
6+
const map = new Map();
7+
8+
// helper to perform the deep copy
9+
const dfs = (currentNode) => {
10+
if (!currentNode) return null;
11+
12+
// if the node has already been copied, return the copied one
13+
if (map.has(currentNode)) return map.get(currentNode);
14+
15+
// create a new node with the current node's value
16+
const newNode = new Node(currentNode.val);
17+
18+
// store this new node in the map
19+
map.set(currentNode, newNode);
20+
21+
// iterate through each neighbor of the current node
22+
for (const neighbor of currentNode.neighbors) {
23+
// clone the neighbors and add them to the new node's neighbors
24+
newNode.neighbors.push(dfs(neighbor));
25+
}
26+
27+
// return the newly created node
28+
return newNode;
29+
};
30+
31+
// start the deep copy from the given node
32+
return dfs(node);
33+
};

course-schedule/yolophg.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Time Complexity: O(numCourses * E) E = Edge
2+
// Space Complexity: O(numCourses * E)
3+
4+
// initialize an array to keep track of the in-degrees for each course
5+
let inDegree = new Array(numCourses).fill(0);
6+
// initialize an adjacency list to represent the graph of courses
7+
let adjList = new Array(numCourses).fill(0).map(() => []);
8+
9+
// fill the in-degree array and adjacency list based on the prerequisites
10+
for (let [course, prereq] of prerequisites) {
11+
// increment the in-degree of the course
12+
inDegree[course]++;
13+
// add the course to the adjacency list of the prerequisite
14+
adjList[prereq].push(course);
15+
}
16+
17+
// initialize a queue to keep track of courses with no prerequisites (in-degree of 0)
18+
let queue = [];
19+
for (let i = 0; i < numCourses; i++) {
20+
if (inDegree[i] === 0) {
21+
queue.push(i);
22+
}
23+
}
24+
25+
// initialize a counter to keep track of the number of courses that have been processed
26+
let count = 0;
27+
28+
// process the courses with no prerequisites
29+
while (queue.length > 0) {
30+
// get a course from the queue
31+
let current = queue.shift();
32+
// increment the counter as this course can be taken
33+
count++;
34+
35+
// iterate through the adjacency list of the current course
36+
for (let neighbor of adjList[current]) {
37+
// decrement the in-degree of the neighboring course
38+
inDegree[neighbor]--;
39+
// if in-degree becomes 0, add it to the queue
40+
if (inDegree[neighbor] === 0) {
41+
queue.push(neighbor);
42+
}
43+
}
44+
}
45+
46+
// if the count of processed courses equals the total number of courses, return true
47+
return count === numCourses;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// AddWord Method : Time Complexity: O(L)/Space Complexity: O(L) L = the length of the word being added
2+
// Search Method : Time Complexity: O(M)/Space Complexity: O(1) M = the length of the word being searched
3+
// SearchInNode Method : Time Complexity: O(M)/Space Complexity: O(1)
4+
5+
var WordDictionary = function () {
6+
this.root = {};
7+
};
8+
9+
WordDictionary.prototype.addWord = function (word) {
10+
// start from the root node
11+
let node = this.root;
12+
for (let char of word) {
13+
if (!node[char]) {
14+
// if the character does not exist, create a new node
15+
node[char] = {};
16+
}
17+
// move to the next node
18+
node = node[char];
19+
}
20+
// mark the end of the word
21+
node.isEndOfWord = true;
22+
};
23+
24+
WordDictionary.prototype.search = function (word) {
25+
// start searching from the root
26+
return this.searchInNode(word, 0, this.root);
27+
};
28+
29+
WordDictionary.prototype.searchInNode = function (word, index, node) {
30+
if (index === word.length) {
31+
// if all characters are checked
32+
return node.isEndOfWord === true;
33+
}
34+
35+
const char = word[index];
36+
if (char === ".") {
37+
//iIf the character is '.', check all possible nodes at this part
38+
for (let key in node) {
39+
if (
40+
key !== "isEndOfWord" &&
41+
this.searchInNode(word, index + 1, node[key])
42+
) {
43+
// if any path matches, return true
44+
return true;
45+
}
46+
}
47+
// no path matched
48+
return false;
49+
} else if (node[char]) {
50+
// if the character exists in the trie
51+
return this.searchInNode(word, index + 1, node[char]);
52+
} else {
53+
// character path does not exist
54+
return false;
55+
}
56+
};

number-of-islands/yolophg.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Time Complexity: O(m * n) m = rows n = cols
2+
// Space Complexity: O(m * n)
3+
4+
var numIslands = function (grid) {
5+
const m = grid.length;
6+
const n = grid[0].length;
7+
let numIslands = 0;
8+
9+
// directions arrays for moving up, down, left, and right
10+
const directions = [
11+
[-1, 0],
12+
[1, 0],
13+
[0, -1],
14+
[0, 1],
15+
];
16+
17+
// BFS function
18+
function bfs(row, col) {
19+
const queue = [[row, col]];
20+
// mark as visited
21+
grid[row][col] = "0";
22+
23+
while (queue.length > 0) {
24+
const [r, c] = queue.shift();
25+
26+
for (const [dr, dc] of directions) {
27+
const nr = r + dr;
28+
const nc = c + dc;
29+
30+
if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === "1") {
31+
// mark as visited
32+
grid[nr][nc] = "0";
33+
queue.push([nr, nc]);
34+
}
35+
}
36+
}
37+
}
38+
39+
// iterate through each cell in the grid
40+
for (let i = 0; i < m; i++) {
41+
for (let j = 0; j < n; j++) {
42+
if (grid[i][j] === "1") {
43+
numIslands++;
44+
// start BFS to mark all connected land cells
45+
bfs(i, j);
46+
}
47+
}
48+
}
49+
50+
return numIslands;
51+
};
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Time Complexity: O(rows + cols)
2+
// Space Complexity: O(rows + cols)
3+
4+
var pacificAtlantic = function (heights) {
5+
const rows = heights.length;
6+
const cols = heights[0].length;
7+
8+
// initialize matrices to keep track of cells reachable by Pacific and Atlantic
9+
const pacific = [];
10+
const atlantic = [];
11+
12+
// create 2D arrays filled with false values
13+
for (let i = 0; i < rows; i++) {
14+
pacific.push(new Array(cols).fill(false));
15+
atlantic.push(new Array(cols).fill(false));
16+
}
17+
18+
// directions for moving up, right, down, left
19+
const directions = [
20+
[0, 1],
21+
[1, 0],
22+
[0, -1],
23+
[-1, 0],
24+
];
25+
26+
// helper to do DFS
27+
function dfs(row, col, ocean) {
28+
// mark the cell as reachable
29+
ocean[row][col] = true;
30+
for (const [dx, dy] of directions) {
31+
// check all 4 directions
32+
const newRow = row + dx;
33+
const newCol = col + dy;
34+
// continue if the new cell is within bounds, not visited, and has a height greater than or equal to the current cell
35+
if (
36+
newRow >= 0 &&
37+
newRow < rows &&
38+
newCol >= 0 &&
39+
newCol < cols &&
40+
!ocean[newRow][newCol] &&
41+
heights[newRow][newCol] >= heights[row][col]
42+
) {
43+
dfs(newRow, newCol, ocean);
44+
}
45+
}
46+
}
47+
48+
// start DFS from the Pacific ocean borders
49+
for (let i = 0; i < rows; i++) {
50+
// left edge
51+
dfs(i, 0, pacific);
52+
// right edge
53+
dfs(i, cols - 1, atlantic);
54+
}
55+
56+
// start DFS from the Atlantic ocean borders
57+
for (let j = 0; j < cols; j++) {
58+
// top edge
59+
dfs(0, j, pacific);
60+
dfs(rows - 1, j, atlantic); // Bottom edge
61+
}
62+
63+
const result = [];
64+
// check each cell can reach both oceans
65+
for (let i = 0; i < rows; i++) {
66+
for (let j = 0; j < cols; j++) {
67+
if (pacific[i][j] && atlantic[i][j]) {
68+
// if it can reach both
69+
result.push([i, j]);
70+
}
71+
}
72+
}
73+
74+
// return the final list
75+
return result;
76+
};

0 commit comments

Comments
 (0)