Skip to content

Commit 4583f0e

Browse files
authored
Merge pull request #812 from Jeehay28/main
[Jeehay28] Week 4
2 parents 451c8c5 + 935f123 commit 4583f0e

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed

coin-change/Jeehay28.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
7+
// TC : O(c*a), where c is the number of coins, and a is amount
8+
// SC : O(a) // dp array requires O(a) space
9+
10+
var coinChange = function (coins, amount) {
11+
// dynamic programming approach
12+
13+
// dp[amount] : the minimum number of coins
14+
// as a default, dp[0] = 0, for other amounts, dp[amount] = amount + 1 ()
15+
// [0, amount+1, amount+1, ...]
16+
const dp = [0, ...new Array(amount).fill(amount + 1)];
17+
18+
// start from coin because i - coin >= 0
19+
for (const coin of coins) {
20+
for (let i = coin; i <= amount; i++) {
21+
// dp[i] : not using the current coin
22+
// dp[i - coin] + 1 : using the current coin
23+
dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
24+
}
25+
}
26+
27+
// dp[amount] === amount + 1 : that amount of money cannot be made up by any combination of the coins
28+
return dp[amount] < amount + 1 ? dp[amount] : -1;
29+
};
30+
31+

merge-two-sorted-lists/Jeehay28.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
14+
// Time Complexity: O(m + n)
15+
// Space Complexity: O(m + n)
16+
17+
var mergeTwoLists = function(list1, list2) {
18+
19+
20+
if(!(list1 && list2)) {
21+
return list1 || list2;
22+
}
23+
24+
if(list1.val < list2.val) {
25+
list1.next = mergeTwoLists(list1.next, list2);
26+
return list1;
27+
} else {
28+
list2.next = mergeTwoLists(list1, list2.next);
29+
return list2;
30+
}
31+
32+
};
33+
34+

missing-number/Jeehay28.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
6+
// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums
7+
// XOR method
8+
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements)
9+
// Space complexity: O(1)
10+
11+
var missingNumber = function (nums) {
12+
// XOR with itself results in 0 : a xor a = 0
13+
// XOR with 0 results in the number itself : a xor 0 = a
14+
// XOR is commutative and associative
15+
16+
const n = nums.length;
17+
18+
let xor = 0;
19+
20+
for (let i = 0; i <= n; i++) {
21+
xor ^= i;
22+
}
23+
24+
for (any of nums) {
25+
xor ^= any;
26+
}
27+
28+
return xor;
29+
};
30+
31+
// *** Guided approach 1: simplicity and clarity
32+
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2
33+
// Time complexity: O(n)
34+
// Space complexity: O(1)
35+
// var missingNumber = function (nums) {
36+
// const n = nums.length;
37+
// const expectedSum = (n * (n + 1)) / 2;
38+
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n)
39+
40+
// const missingNum = expectedSum - actualSum;
41+
42+
// return missingNum;
43+
// };
44+
45+
// *** My own approach
46+
// Time complexity: O(n^2)
47+
// Space complexity: O(n)
48+
// var missingNumber = function (nums) {
49+
50+
// let distinctNums = new Set([]);
51+
52+
// for (any of nums) {
53+
// if (distinctNums.has(any)) {
54+
// return
55+
// } else {
56+
// distinctNums.add(any)
57+
// }
58+
// }
59+
60+
// const n = distinctNums.size;
61+
62+
// for (let i = 0; i <= n; i++) {
63+
// if (!nums.includes(i)) {
64+
// return i;
65+
// }
66+
// }
67+
68+
// };

palindromic-substrings/Jeehay28.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
6+
// TC : O(n^2)
7+
// SC : O(1)
8+
9+
var countSubstrings = function (s) {
10+
// For each character in the string, treat it as the center of a potential palindrome.
11+
12+
// 'Count Palindromic Substrings' helper function
13+
const countPS = (left, right) => {
14+
let cnt = 0;
15+
while (left >= 0 && right < s.length && s[left] === s[right]) {
16+
cnt += 1;
17+
left -= 1;
18+
right += 1;
19+
}
20+
return cnt;
21+
};
22+
23+
let totCnt = 0;
24+
25+
for (let i = 0; i < s.length; i++) {
26+
// left === right : 1 center point, odd-length palindromic
27+
totCnt += countPS(i, i);
28+
29+
// left !== right : 2 center points, even-length palindromic
30+
totCnt += countPS(i, i + 1);
31+
}
32+
33+
return totCnt;
34+
};
35+
36+
37+

word-search/Jeehay28.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
7+
// board(N * M), where N is the number of row and M is the number of columns
8+
// L, the length of the word
9+
// TC : O(N * M * 4^L)
10+
11+
// recursion depth : the length of the word (L)
12+
// each recursion call requires constant space.
13+
// SC : O(L)
14+
15+
var exist = function (board, word) {
16+
let row = board.length;
17+
let col = board[0].length;
18+
19+
const dfs = (r, c, idx) => {
20+
// search done
21+
if (idx === word.length) {
22+
return true;
23+
}
24+
25+
// row and column are out of range
26+
if (r < 0 || r >= row || c < 0 || c >= col) {
27+
return false;
28+
}
29+
30+
if (board[r][c] !== word[idx]) {
31+
return false;
32+
}
33+
34+
// word[idx] === board[r][c]
35+
// continue searching for word[idx + 1] in adjacent cells on the board
36+
const temp = board[r][c];
37+
board[r][c] = "visited";
38+
39+
const arr = [
40+
[1, 0], // Move down
41+
[-1, 0], // Move up
42+
[0, 1], // Move right
43+
[0, -1], // Move left
44+
];
45+
for (const [up, right] of arr) {
46+
if (dfs(r + up, c + right, idx + 1)) {
47+
return true;
48+
}
49+
}
50+
51+
board[r][c] = temp;
52+
return false;
53+
};
54+
55+
for (let i = 0; i < row; i++) {
56+
for (let j = 0; j < col; j++) {
57+
if (dfs(i, j, 0)) {
58+
return true;
59+
}
60+
}
61+
}
62+
63+
return false;
64+
};
65+
66+
67+

0 commit comments

Comments
 (0)