-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] Week 4 #812
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
[Jeehay28] Week 4 #812
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,31 @@ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
|
||
// TC : O(c*a), where c is the number of coins, and a is amount | ||
// SC : O(a) // dp array requires O(a) space | ||
|
||
var coinChange = function (coins, amount) { | ||
// dynamic programming approach | ||
|
||
// dp[amount] : the minimum number of coins | ||
// as a default, dp[0] = 0, for other amounts, dp[amount] = amount + 1 () | ||
// [0, amount+1, amount+1, ...] | ||
const dp = [0, ...new Array(amount).fill(amount + 1)]; | ||
|
||
// start from coin because i - coin >= 0 | ||
for (const coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
// dp[i] : not using the current coin | ||
// dp[i - coin] + 1 : using the current coin | ||
dp[i] = Math.min(dp[i - coin] + 1, dp[i]); | ||
} | ||
} | ||
|
||
// dp[amount] === amount + 1 : that amount of money cannot be made up by any combination of the coins | ||
return dp[amount] < amount + 1 ? dp[amount] : -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,34 @@ | ||
/** | ||
* 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} | ||
*/ | ||
|
||
// Time Complexity: O(m + n) | ||
// Space Complexity: O(m + n) | ||
|
||
var mergeTwoLists = function(list1, list2) { | ||
|
||
|
||
if(!(list1 && list2)) { | ||
return list1 || list2; | ||
} | ||
|
||
if(list1.val < list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1; | ||
} else { | ||
list2.next = mergeTwoLists(list1, list2.next); | ||
return list2; | ||
} | ||
|
||
}; | ||
|
||
|
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,68 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
|
||
// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums | ||
// XOR method | ||
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements) | ||
// Space complexity: O(1) | ||
|
||
var missingNumber = function (nums) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오.. XOR 풀이도 신기하네요!
|
||
// XOR with itself results in 0 : a xor a = 0 | ||
// XOR with 0 results in the number itself : a xor 0 = a | ||
// XOR is commutative and associative | ||
|
||
const n = nums.length; | ||
|
||
let xor = 0; | ||
|
||
for (let i = 0; i <= n; i++) { | ||
xor ^= i; | ||
} | ||
|
||
for (any of nums) { | ||
xor ^= any; | ||
} | ||
|
||
return xor; | ||
}; | ||
|
||
// *** Guided approach 1: simplicity and clarity | ||
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2 | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
// var missingNumber = function (nums) { | ||
// const n = nums.length; | ||
// const expectedSum = (n * (n + 1)) / 2; | ||
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n) | ||
|
||
// const missingNum = expectedSum - actualSum; | ||
|
||
// return missingNum; | ||
// }; | ||
|
||
// *** My own approach | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
// var missingNumber = function (nums) { | ||
|
||
// let distinctNums = new Set([]); | ||
|
||
// for (any of nums) { | ||
// if (distinctNums.has(any)) { | ||
// return | ||
// } else { | ||
// distinctNums.add(any) | ||
// } | ||
// } | ||
|
||
// const n = distinctNums.size; | ||
|
||
// for (let i = 0; i <= n; i++) { | ||
// if (!nums.includes(i)) { | ||
// return i; | ||
// } | ||
// } | ||
|
||
// }; |
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,37 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
|
||
// TC : O(n^2) | ||
// SC : O(1) | ||
|
||
var countSubstrings = function (s) { | ||
// For each character in the string, treat it as the center of a potential palindrome. | ||
|
||
// 'Count Palindromic Substrings' helper function | ||
const countPS = (left, right) => { | ||
let cnt = 0; | ||
while (left >= 0 && right < s.length && s[left] === s[right]) { | ||
cnt += 1; | ||
left -= 1; | ||
right += 1; | ||
} | ||
return cnt; | ||
}; | ||
|
||
let totCnt = 0; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
// left === right : 1 center point, odd-length palindromic | ||
totCnt += countPS(i, i); | ||
|
||
// left !== right : 2 center points, even-length palindromic | ||
totCnt += countPS(i, i + 1); | ||
} | ||
|
||
return totCnt; | ||
}; | ||
|
||
|
||
|
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,67 @@ | ||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
|
||
// board(N * M), where N is the number of row and M is the number of columns | ||
// L, the length of the word | ||
// TC : O(N * M * 4^L) | ||
|
||
// recursion depth : the length of the word (L) | ||
// each recursion call requires constant space. | ||
// SC : O(L) | ||
|
||
var exist = function (board, word) { | ||
let row = board.length; | ||
let col = board[0].length; | ||
|
||
const dfs = (r, c, idx) => { | ||
// search done | ||
if (idx === word.length) { | ||
return true; | ||
} | ||
|
||
// row and column are out of range | ||
if (r < 0 || r >= row || c < 0 || c >= col) { | ||
return false; | ||
} | ||
|
||
if (board[r][c] !== word[idx]) { | ||
return false; | ||
} | ||
|
||
// word[idx] === board[r][c] | ||
// continue searching for word[idx + 1] in adjacent cells on the board | ||
const temp = board[r][c]; | ||
board[r][c] = "visited"; | ||
|
||
const arr = [ | ||
[1, 0], // Move down | ||
[-1, 0], // Move up | ||
[0, 1], // Move right | ||
[0, -1], // Move left | ||
]; | ||
for (const [up, right] of arr) { | ||
if (dfs(r + up, c + right, idx + 1)) { | ||
return true; | ||
} | ||
} | ||
|
||
board[r][c] = temp; | ||
return false; | ||
}; | ||
|
||
for (let i = 0; i < row; i++) { | ||
for (let j = 0; j < col; j++) { | ||
if (dfs(i, j, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My own approach부분도 잘 봤습니다
문제의 전제가 중복된 숫자가 없는 배열이라
let distinctNums = new Set(nums);으로 작성해도 동작하네요