-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week4 #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[ganu] Week4 #809
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4213391
feat: 21. Merge Two Sorted Lists
gwbaik9717 e97aff4
feat: 268. Missing Number
gwbaik9717 43853fc
feat: 79. Word Search
gwbaik9717 32c4b56
feat: 647. Palindromic Substrings
gwbaik9717 ca72dd4
feat: 322. Coin Change
gwbaik9717 0cdbba8
refactor: feat: 21. Merge Two Sorted Lists
gwbaik9717 70f3ef1
refactor: feat: 79. Word Search
gwbaik9717 6eb0ac0
refactor: feat: 322. Coin Change
gwbaik9717 7812d91
refactor: 21. Merge Two Sorted Lists
gwbaik9717 e7c0e56
refactor: 647. Palindromic Substrings
gwbaik9717 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.