-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #809 from gwbaik9717/main
[ganu] Week4
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// n: amount m: length of coins | ||
// Time complexity: O(n * m) + O(mlogm) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function (coins, amount) { | ||
const dp = Array.from({ length: amount + 1 }, () => Infinity); | ||
dp[0] = 0; | ||
|
||
coins.sort((a, b) => a - b); | ||
|
||
for (const coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
|
||
return dp.at(-1) === Infinity ? -1 : dp.at(-1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// m: list1, n: list2 | ||
// Time complexity: O(m+n) | ||
// Space complexity: O(m+n) | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} list1 | ||
* @param {ListNode} list2 | ||
* @return {ListNode} | ||
*/ | ||
var mergeTwoLists = function (list1, list2) { | ||
const answer = new ListNode(); | ||
let current = answer; | ||
|
||
while (list1 && list2) { | ||
if (list1.val < list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
|
||
current = current.next; | ||
} | ||
|
||
if (list1) { | ||
current.next = list1; | ||
} | ||
|
||
if (list2) { | ||
current.next = list2; | ||
} | ||
|
||
return answer.next; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var missingNumber = function (nums) { | ||
const n = nums.length; | ||
const target = (n * (n + 1)) / 2; | ||
|
||
const sum = nums.reduce((a, c) => a + c, 0); | ||
|
||
return target - sum; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n^2) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var countSubstrings = function (s) { | ||
const n = s.length; | ||
const dp = Array.from({ length: n }, () => | ||
Array.from({ length: n }, () => false) | ||
); | ||
let answer = 0; | ||
|
||
for (let end = 0; end < n; end++) { | ||
for (let start = end; start >= 0; start--) { | ||
if (start === end) { | ||
dp[start][end] = true; | ||
answer++; | ||
continue; | ||
} | ||
|
||
if (start + 1 === end) { | ||
if (s[start] === s[end]) { | ||
dp[start][end] = true; | ||
answer++; | ||
} | ||
continue; | ||
} | ||
|
||
if (s[start] === s[end] && dp[start + 1][end - 1]) { | ||
dp[start][end] = true; | ||
answer++; | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// h: height of the board, w: width of the board, n: length of the word | ||
// Time complexity: O(h * w * 4**n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
var exist = function (board, word) { | ||
const n = word.length; | ||
const h = board.length; | ||
const w = board[0].length; | ||
|
||
const dy = [1, 0, -1, 0]; | ||
const dx = [0, 1, 0, -1]; | ||
|
||
let answer = false; | ||
|
||
const dfs = (current, index) => { | ||
if (index === n - 1) { | ||
answer = true; | ||
return; | ||
} | ||
|
||
const [cy, cx] = current; | ||
const value = board[cy][cx]; | ||
board[cy][cx] = ""; | ||
|
||
for (let i = 0; i < dy.length; i++) { | ||
const ny = cy + dy[i]; | ||
const nx = cx + dx[i]; | ||
const ni = index + 1; | ||
|
||
if ( | ||
ny >= 0 && | ||
ny < h && | ||
nx >= 0 && | ||
nx < w && | ||
board[ny][nx] && | ||
word[ni] === board[ny][nx] | ||
) { | ||
dfs([ny, nx], ni); | ||
} | ||
} | ||
|
||
board[cy][cx] = value; | ||
}; | ||
|
||
for (let i = 0; i < h; i++) { | ||
for (let j = 0; j < w; j++) { | ||
if (board[i][j] === word[0] && !answer) { | ||
dfs([i, j], 0); | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
}; |