-
Notifications
You must be signed in to change notification settings - Fork 126
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
[kimyoung] WEEK 03 Solutions #389
Changes from all commits
3810e50
4e17a73
6dff871
0363257
f5536b5
b4b98e9
57b9908
f5cfc08
e5d46b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// found a pattern where the result is the addition of two previous elements, | ||
// but not sure if this is the expected answer | ||
var climbStairs = function (n) { | ||
let arr = new Array(n).fill(1); | ||
for (let i = 2; i <= n; i++) { | ||
arr[i] = arr[i - 2] + arr[i - 1]; | ||
} | ||
return n === 1 ? 1 : arr[n]; | ||
}; | ||
|
||
// time - O(n) iterate up to n times | ||
// space - O(n) creates an array upto n elements |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||||||||||||||||||
var coinChange = function (coins, amount) { | ||||||||||||||||||||||||
// create a dp array that stores the minimum amount of coins used for each amount leading up to the target amount | ||||||||||||||||||||||||
// fill with array with amount + 1 as a default | ||||||||||||||||||||||||
const dp = [0, ...new Array(amount).fill(amount + 1)]; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// loop through the coins | ||||||||||||||||||||||||
for (const coin of coins) { | ||||||||||||||||||||||||
// for each amount, assess how the current coin can modify the existing value within the dp array by comparing the min value | ||||||||||||||||||||||||
for (let i = 1; i <= amount; i++) { | ||||||||||||||||||||||||
// only works if coin is less than or equal to the assessing amount | ||||||||||||||||||||||||
if (coin <= i) { | ||||||||||||||||||||||||
// min comparison | ||||||||||||||||||||||||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+9
to
+15
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.
Suggested change
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. 이제보니 이렇게 최적화가 가능했군요. 앞으로는 코드 마무리 후에 최적화 할수 있는 방법도 고민해보도록 하겠습니다! |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
// check target amount in dp array to see if it's the default value | ||||||||||||||||||||||||
// if it's default value, it means coin combination cannot lead up to target amount | ||||||||||||||||||||||||
// if it's not default value, that is the minimum required coin change to lead up to target amount | ||||||||||||||||||||||||
return dp[amount] === amount + 1 ? -1 : dp[amount]; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// time - O(a * c) loops through amount * loops through coin | ||||||||||||||||||||||||
// space - O(a) depends on the size of amount |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var combinationSum = function (candidates, target) { | ||
let results = []; | ||
|
||
function helper(idx, curr, total) { | ||
// base case - when total = target push into results | ||
if (total === target) { | ||
results.push([...curr]); | ||
return; | ||
} | ||
// base exit case - when the index is greater or equal to candidates or the total is greater than target, exit | ||
if (idx >= candidates.length || total > target) { | ||
return; | ||
} | ||
|
||
// recursive case | ||
// case where we include the current value without advancing the index | ||
curr.push(candidates[idx]); | ||
helper(idx, curr, total + candidates[idx]); | ||
curr.pop() | ||
// case where we advance the index | ||
helper(idx + 1, curr, total); | ||
} | ||
helper(0, [], 0); | ||
|
||
return results; | ||
}; | ||
|
||
// time - O(2^n) backtracking | ||
// space - O(1) | ||
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. 혹시 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var productExceptSelf = function (nums) { | ||
let result = new Array(nums.length).fill(0); // create a result array of length num | ||
let pre = 1, post = 1; // default to 1 | ||
|
||
for (let i = 0; i < nums.length; i++) { // fill the result array with prefix (multiplication of left values) | ||
result[i] = pre; | ||
pre *= nums[i]; | ||
} | ||
for (let i = nums.length - 1; i >= 0; i--) { // multiply the postfix (multiplication of right values) to the result array in their corresponding index | ||
result[i] *= post; | ||
post *= nums[i]; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// time - O(n) iterate through the nums array twice - 2n, remove constant which ends up to be n | ||
// space - O(1) result array not part of space complexity |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var twoSum = function (nums, target) { | ||
let map = new Map(); // create a map to store the number and index of each element | ||
for(let i = 0; i < nums.length; i++) { | ||
let diff = target - nums[i]; | ||
if(map.has(diff)) { // once the differnece is found, return both index | ||
return [i, map.get(diff)]; | ||
} else { // otherwise add to map | ||
map.set(nums[i], i) | ||
} | ||
} | ||
}; | ||
|
||
// time - O(n) at worst, iterate through the entire nums array | ||
// space - O(n) at worst, map the entire nums array |
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.
안녕하세요 @kim-young 님!
n이 1인지 확인하는 부분은 앞쪽에서 early return 해 주면 가독성이 좀 더 좋아지지 않을까요?
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.
좋은 리뷰 감사합니다! 이제보니 return statement가 가독성이 많이 떨어지네요. 😂