Skip to content

Commit 390e210

Browse files
authored
Merge pull request #809 from gwbaik9717/main
[ganu] Week4
2 parents b8c3521 + e7c0e56 commit 390e210

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

coin-change/gwbaik9717.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// n: amount m: length of coins
2+
// Time complexity: O(n * m) + O(mlogm)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {number[]} coins
7+
* @param {number} amount
8+
* @return {number}
9+
*/
10+
var coinChange = function (coins, amount) {
11+
const dp = Array.from({ length: amount + 1 }, () => Infinity);
12+
dp[0] = 0;
13+
14+
coins.sort((a, b) => a - b);
15+
16+
for (const coin of coins) {
17+
for (let i = coin; i <= amount; i++) {
18+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
19+
}
20+
}
21+
22+
return dp.at(-1) === Infinity ? -1 : dp.at(-1);
23+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// m: list1, n: list2
2+
// Time complexity: O(m+n)
3+
// Space complexity: O(m+n)
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* function ListNode(val, next) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.next = (next===undefined ? null : next)
10+
* }
11+
*/
12+
/**
13+
* @param {ListNode} list1
14+
* @param {ListNode} list2
15+
* @return {ListNode}
16+
*/
17+
var mergeTwoLists = function (list1, list2) {
18+
const answer = new ListNode();
19+
let current = answer;
20+
21+
while (list1 && list2) {
22+
if (list1.val < list2.val) {
23+
current.next = list1;
24+
list1 = list1.next;
25+
} else {
26+
current.next = list2;
27+
list2 = list2.next;
28+
}
29+
30+
current = current.next;
31+
}
32+
33+
if (list1) {
34+
current.next = list1;
35+
}
36+
37+
if (list2) {
38+
current.next = list2;
39+
}
40+
41+
return answer.next;
42+
};

missing-number/gwbaik9717.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var missingNumber = function (nums) {
9+
const n = nums.length;
10+
const target = (n * (n + 1)) / 2;
11+
12+
const sum = nums.reduce((a, c) => a + c, 0);
13+
14+
return target - sum;
15+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Time complexity: O(n^2)
2+
// Space complexity: O(n^2)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var countSubstrings = function (s) {
9+
const n = s.length;
10+
const dp = Array.from({ length: n }, () =>
11+
Array.from({ length: n }, () => false)
12+
);
13+
let answer = 0;
14+
15+
for (let end = 0; end < n; end++) {
16+
for (let start = end; start >= 0; start--) {
17+
if (start === end) {
18+
dp[start][end] = true;
19+
answer++;
20+
continue;
21+
}
22+
23+
if (start + 1 === end) {
24+
if (s[start] === s[end]) {
25+
dp[start][end] = true;
26+
answer++;
27+
}
28+
continue;
29+
}
30+
31+
if (s[start] === s[end] && dp[start + 1][end - 1]) {
32+
dp[start][end] = true;
33+
answer++;
34+
continue;
35+
}
36+
}
37+
}
38+
39+
return answer;
40+
};

word-search/gwbaik9717.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// h: height of the board, w: width of the board, n: length of the word
2+
// Time complexity: O(h * w * 4**n)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {character[][]} board
7+
* @param {string} word
8+
* @return {boolean}
9+
*/
10+
var exist = function (board, word) {
11+
const n = word.length;
12+
const h = board.length;
13+
const w = board[0].length;
14+
15+
const dy = [1, 0, -1, 0];
16+
const dx = [0, 1, 0, -1];
17+
18+
let answer = false;
19+
20+
const dfs = (current, index) => {
21+
if (index === n - 1) {
22+
answer = true;
23+
return;
24+
}
25+
26+
const [cy, cx] = current;
27+
const value = board[cy][cx];
28+
board[cy][cx] = "";
29+
30+
for (let i = 0; i < dy.length; i++) {
31+
const ny = cy + dy[i];
32+
const nx = cx + dx[i];
33+
const ni = index + 1;
34+
35+
if (
36+
ny >= 0 &&
37+
ny < h &&
38+
nx >= 0 &&
39+
nx < w &&
40+
board[ny][nx] &&
41+
word[ni] === board[ny][nx]
42+
) {
43+
dfs([ny, nx], ni);
44+
}
45+
}
46+
47+
board[cy][cx] = value;
48+
};
49+
50+
for (let i = 0; i < h; i++) {
51+
for (let j = 0; j < w; j++) {
52+
if (board[i][j] === word[0] && !answer) {
53+
dfs([i, j], 0);
54+
}
55+
}
56+
}
57+
58+
return answer;
59+
};

0 commit comments

Comments
 (0)