Skip to content
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

Merged
merged 9 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions climbing-stairs/kimyoung.js
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];
Copy link
Contributor

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 해 주면 가독성이 좀 더 좋아지지 않을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 리뷰 감사합니다! 이제보니 return statement가 가독성이 많이 떨어지네요. 😂

};

// time - O(n) iterate up to n times
// space - O(n) creates an array upto n elements
24 changes: 24 additions & 0 deletions coin-change/kimyoung.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

icoin부터 시작하면 굳이 coin <= i를 체크해줄 필요가 없지 않을까 생각이 들었습니다.

Suggested change
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);
}
}
for (let i = coin; i <= amount; i++) {
// min comparison
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
29 changes: 29 additions & 0 deletions combination-sum/kimyoung.js
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 curr 배열의 메모리 사용량을 고려하셨나요?

18 changes: 18 additions & 0 deletions product-of-array-except-self/kimyoung.js
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
14 changes: 14 additions & 0 deletions two-sum/kimyoung.js
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