Skip to content

Commit 82f5fbc

Browse files
authored
Merge pull request #933 from Jeehay28/main
[Jeehay28] WEEK 07
2 parents e0edb9d + b49316a commit 82f5fbc

File tree

5 files changed

+388
-0
lines changed

5 files changed

+388
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
6+
// 🌟 sliding window technique
7+
8+
// Time Complexity: O(n)
9+
// Why it's O(n)
10+
// - The end pointer iterates over the string exactly once (O(n)).
11+
// - The start pointer also only moves forward without re-processing elements (O(n)).
12+
// - Therefore, the total amount of work done is proportional to the length of the string (n).
13+
// - So, even though there is a while loop inside the for loop, the total work done (number of operations) is still linear, O(n), because the start and end pointers together move across the string just once.
14+
// - This is the key reason why the time complexity is O(n), even with nested loops.
15+
16+
// Space Complexity: O(k), where k k is the length of the longest substring without repeating characters.
17+
// In the worst case, k = n, so O(n)
18+
19+
var lengthOfLongestSubstring = function (s) {
20+
let start = 0;
21+
let longest = 0;
22+
let subString = new Set();
23+
24+
for (let end = 0; end < s.length; end++) {
25+
while (subString.has(s[end])) {
26+
// Shrink the window by moving start
27+
subString.delete(s[start]);
28+
start += 1;
29+
}
30+
31+
subString.add(s[end]);
32+
longest = Math.max(longest, end - start + 1);
33+
}
34+
35+
return longest;
36+
};
37+
38+
// 🛠️ Solution 1
39+
// Time Complexity: O(n^2), where n is the length of the string s
40+
// Space Complexity: O(k), where k is the length of the longest substring without repeating characters (k ≤ n)
41+
42+
// why the space complexity is not just O(n):
43+
// - Saying O(n) is technically correct in the worst case,
44+
// - but it hides the fact that the actual space usage is proportional to the length of the longest substring without repeats,
45+
// - which could be much smaller than n in many practical cases (e.g., for a string like "aaabbbccc").
46+
47+
// var lengthOfLongestSubstring = function (s) {
48+
// let longest = 0;
49+
50+
// for (let i = 0; i < s.length; i++) {
51+
// let subString = new Set();
52+
53+
// for (let j = i; j < s.length; j++) {
54+
// if (subString.has(s[j])) {
55+
// break;
56+
// } else {
57+
// subString.add(s[j]);
58+
// longest = Math.max(longest, j - i + 1);
59+
// }
60+
// }
61+
// }
62+
// return longest;
63+
// };
64+

number-of-islands/Jeehay28.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
6+
// 🌻
7+
// Time Complexity: O(m * n), where M is the number of rows and N is the number of columns in the grid.
8+
// Space Complexity: O(k), where k is the size of the largest island (k <= m * n)
9+
10+
// The space complexity is determined by the depth of the recursive stack used by the sink function.
11+
// In the worst-case scenario, where the entire grid is filled with "1"s (a single large island),
12+
// the depth of recursion could go up to O(m * n).
13+
14+
var numIslands = function (grid) {
15+
// depth-first search (DFS) that potentially visits every connected cell in the current island
16+
const sink = (row, col) => {
17+
grid[row][col] = "0";
18+
19+
const adjacent = [
20+
[row - 1, col], // up
21+
[row + 1, col], // down
22+
[row, col - 1], // left
23+
[row, col + 1], // right
24+
];
25+
26+
for ([r, c] of adjacent) {
27+
if (r >= 0 && r < grid.length && c >= 0 && c < grid[r].length) {
28+
if (grid[r][c] === "1") {
29+
sink(r, c);
30+
}
31+
}
32+
}
33+
};
34+
35+
let cnt = 0;
36+
37+
for (let i = 0; i < grid.length; i++) {
38+
for (let j = 0; j < grid[0].length; j++) {
39+
if (grid[i][j] === "1") {
40+
cnt += 1;
41+
sink(i, j);
42+
}
43+
}
44+
}
45+
46+
return cnt;
47+
};
48+
49+
50+

reverse-linked-list/Jeehay28.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
13+
// Time Complexity: O(n)
14+
// Space Complexity: O(1)
15+
16+
// The algorithm uses a constant amount of extra space: prev, current, and nextTemp.
17+
// No additional data structures (like arrays or new linked lists) are created.
18+
// Hence, the space complexity is O(1).
19+
20+
var reverseList = function (head) {
21+
let prev = null;
22+
let current = head;
23+
24+
while (current) {
25+
let nextTemp = current.next;
26+
27+
current.next = prev;
28+
console.log(current, prev);
29+
30+
prev = current;
31+
console.log(current, prev);
32+
current = nextTemp;
33+
}
34+
35+
return prev; // New head of the reversed list
36+
};
37+
38+
// head = [1,2,3,4,5]
39+
// [1] null
40+
// [1] [1]
41+
// [2,1] [1]
42+
// [2,1] [2,1]
43+
// [3,2,1] [2,1]
44+
// [3,2,1] [3,2,1]
45+
// [4,3,2,1] [3,2,1]
46+
// [4,3,2,1] [4,3,2,1]
47+
// [5,4,3,2,1] [5,4,3,2,1]
48+
49+
// my own approach
50+
// Time Complexity: O(n)
51+
// Space Complexity: O(n)
52+
var reverseList = function (head) {
53+
if (head === null) {
54+
return null;
55+
}
56+
57+
let current = head;
58+
let arr = [];
59+
// console.log(head.val)
60+
61+
// traverse the linked List - TC: O(n)
62+
while (current) {
63+
arr.push(current.val);
64+
current = current.next;
65+
}
66+
67+
// reverse the array - TC: O(n)
68+
arr = arr.reverse();
69+
70+
let head1 = new ListNode(arr[0]);
71+
let current1 = head1;
72+
73+
// rebuild the linked list - TC: O(n)
74+
for (let i = 1; i < arr.length; i++) {
75+
current1.next = new ListNode(arr[i]);
76+
current1 = current1.next;
77+
}
78+
79+
return head1;
80+
};
81+
82+

set-matrix-zeroes/Jeehay28.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// 🌈 In-Place Solution (Without extra space)
2+
// The term "in-place" refers to algorithms or solutions that modify the input data directly,
3+
// without needing extra space for a separate copy of the data. In an in-place solution,
4+
// the operations are performed using a fixed amount of extra memory, typically O(1) space, beyond the input data itself.
5+
6+
/**
7+
* @param {number[][]} matrix
8+
* @return {void} Do not return anything, modify matrix in-place instead.
9+
*/
10+
11+
// Time Complexity: O(m * n)
12+
// Space Complexity: O(1)
13+
var setZeroes = function (matrix) {
14+
const rows = matrix.length;
15+
const cols = matrix[0].length;
16+
17+
let firstRowHasZero = false;
18+
let firstColHasZero = false;
19+
20+
// Check if the first row has any zero
21+
for (let c = 0; c < cols; c++) {
22+
if (matrix[0][c] === 0) {
23+
firstRowHasZero = true;
24+
break;
25+
}
26+
}
27+
28+
// Check if the first column has any zero
29+
for (let r = 0; r < rows; r++) {
30+
if (matrix[r][0] === 0) {
31+
firstColHasZero = true;
32+
break;
33+
}
34+
}
35+
36+
// Use first row and column to mark zeros
37+
for (let i = 1; i < rows; i++) {
38+
for (let j = 1; j < cols; j++) {
39+
if (matrix[i][j] === 0) {
40+
matrix[0][j] = 0;
41+
matrix[i][0] = 0;
42+
}
43+
}
44+
}
45+
46+
// Set zeros based on marks in the first row and column
47+
for (let i = 1; i < rows; i++) {
48+
for (let j = 1; j < cols; j++) {
49+
if (matrix[0][j] === 0 || matrix[i][0] === 0) {
50+
matrix[i][j] = 0;
51+
}
52+
}
53+
}
54+
55+
// Handle first row
56+
if (firstRowHasZero) {
57+
for (let c = 0; c < cols; c++) {
58+
matrix[0][c] = 0;
59+
}
60+
}
61+
62+
// Handle first column
63+
if (firstColHasZero) {
64+
for (let r = 0; r < rows; r++) {
65+
matrix[r][0] = 0;
66+
}
67+
}
68+
69+
return matrix;
70+
};
71+
72+
/**
73+
* @param {number[][]} matrix
74+
* @return {void} Do not return anything, modify matrix in-place instead.
75+
*/
76+
77+
// 💪 My initial approach with Set...
78+
// Time Complexity: O(m * n)
79+
// Space Complexity: O(m + n)
80+
// var setZeroes = function (matrix) {
81+
// let rows = new Set();
82+
// let cols = new Set();
83+
84+
// for (let i = 0; i < matrix.length; i++) {
85+
// for (let j = 0; j < matrix[0].length; j++) {
86+
// if (matrix[i][j] === 0) {
87+
// rows.add(i);
88+
// cols.add(j);
89+
// }
90+
// }
91+
// }
92+
93+
// for (row of rows) {
94+
// matrix[row] = new Array(matrix[0].length).fill(0);
95+
// }
96+
97+
// for (col of cols) {
98+
// for (let row = 0; row < matrix.length; row++) {
99+
// matrix[row][col] = 0;
100+
// }
101+
// }
102+
103+
// return matrix;
104+
// };
105+
106+

unique-paths/Jeehay28.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @param {number} m
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
7+
// 🍎 DP
8+
// Time Complexity: O(m*n)
9+
// Space Complexity: O(n)
10+
11+
// Row 0: [1, 1, 1, 1]
12+
// Row 1: [1, 2, 3, 4]
13+
// Row 2: [1, 3, 6, 10]
14+
15+
// Initial dp: [1, 1, 1, 1]
16+
// left = 1 (always starts with 1 for the first column)
17+
18+
// col = 1: left += dp[1] → left = 1 + 1 = 2 → dp[1] = left
19+
// col = 2: left += dp[2] → left = 2 + 1 = 3 → dp[2] = left
20+
// col = 3: left += dp[3] → left = 3 + 1 = 4 → dp[3] = left
21+
22+
// dp after row 1: [1, 2, 3, 4]
23+
24+
// Initial dp: [1, 2, 3, 4]
25+
// left = 1
26+
27+
// col = 1: left += dp[1] → left = 1 + 2 = 3 → dp[1] = left
28+
// col = 2: left += dp[2] → left = 3 + 3 = 6 → dp[2] = left
29+
// col = 3: left += dp[3] → left = 6 + 4 = 10 → dp[3] = left
30+
31+
// dp after row 2: [1, 3, 6, 10]
32+
33+
var uniquePaths = function (m, n) {
34+
let dp = new Array(n).fill(1);
35+
36+
for (let row = 1; row < m; row++) {
37+
let left = 1;
38+
for (let col = 1; col < n; col++) {
39+
left += dp[col];
40+
dp[col] = left;
41+
}
42+
}
43+
44+
return dp[n - 1];
45+
};
46+
47+
/**
48+
* @param {number} m
49+
* @param {number} n
50+
* @return {number}
51+
*/
52+
53+
// 🍎 DFS
54+
// Time Complexity: O(m*n)
55+
// Space Complexity: O(m*n)
56+
57+
// var uniquePaths = function (m, n) {
58+
// const memo = {};
59+
60+
// const dfs = (row, col) => {
61+
// if (row === m - 1 && col === n - 1) {
62+
// return 1;
63+
// }
64+
65+
// let cnt = 0;
66+
67+
// const key = `${row}, ${col}`;
68+
// if (key in memo) {
69+
// return memo[key];
70+
// }
71+
72+
// if (row + 1 < m) {
73+
// cnt += dfs(row + 1, col);
74+
// }
75+
76+
// if (col + 1 < n) {
77+
// cnt += dfs(row, col + 1);
78+
// }
79+
80+
// memo[key] = cnt;
81+
82+
// return cnt;
83+
// };
84+
85+
// return dfs(0, 0);
86+
// };

0 commit comments

Comments
 (0)