-
-
Notifications
You must be signed in to change notification settings - Fork 195
[선재] WEEK 03 #391
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
[선재] WEEK 03 #391
Changes from all commits
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,18 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* Dynamic Programming | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
|
||
var climbStairs = function (n) { | ||
const dp = Array.from({ length: n + 1 }, () => 0); | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
|
||
for (let i = 3; i < n + 1; i++) dp[i] = dp[i - 1] + dp[i - 2]; | ||
|
||
return dp[n]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. asc sort + division calculate | ||
* 2. bfs + memoization | ||
* | ||
* strategy: | ||
* bfs + memoization | ||
* | ||
* reason: | ||
* Tried with brainstorming 1 but test case is false | ||
* | ||
* time complexity: O(n^k) | ||
* space complexity: O(n) | ||
*/ | ||
class Node { | ||
constructor(val) { | ||
this.value = val; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class CustomQueue { | ||
constructor() { | ||
this.front = null; | ||
this.rear = null; | ||
this.size = 0; | ||
} | ||
|
||
push(val) { | ||
const node = new Node(val); | ||
|
||
if (this.size === 0) { | ||
this.front = node; | ||
this.rear = node; | ||
} else { | ||
this.rear.next = node; | ||
this.rear = node; | ||
} | ||
|
||
this.size++; | ||
} | ||
|
||
pop() { | ||
if (this.size === 0) return null; | ||
const node = this.front; | ||
this.front = this.front.next; | ||
this.size--; | ||
if (this.size === 0) this.rear = null; | ||
|
||
return node.value; | ||
} | ||
} | ||
Comment on lines
+23
to
+53
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. 자바스크립트에 내장 큐 자료구조가 없어서 이렇게 직접 짜셔야하는 거 겠죠? 🥲 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. 맞아요 ㅠㅠ |
||
|
||
var coinChange = function (coins, amount) { | ||
const queue = new CustomQueue(); | ||
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. 저는 개인적으로 DP를 굉장히 못 풀어서 DP로 풀 수 있는 문제같으면 답안을 꼭 참고하는 편입니다 ㅎㅎ 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. DP 초보인데 레퍼런스 감사합니다! |
||
const memoSet = new Set(); | ||
|
||
if (amount === 0) return 0; | ||
|
||
for (const coin of coins) { | ||
if (amount === coin) return 1; | ||
|
||
queue.push(coin); | ||
memoSet.add(coin); | ||
} | ||
|
||
let count = 1; | ||
|
||
while (queue.size) { | ||
count++; | ||
let depthSize = queue.size; | ||
|
||
while (depthSize--) { | ||
const sum = queue.pop(); | ||
|
||
for (const coin of coins) { | ||
const nextSum = sum + coin; | ||
|
||
if (memoSet.has(nextSum)) continue; | ||
if (amount === nextSum) return count; | ||
if (amount > nextSum) queue.push(nextSum); | ||
|
||
memoSet.add(nextSum); | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* dfs | ||
* | ||
* time complexity: O(n^k) | ||
* space complexity: O(n) | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
const answer = []; | ||
|
||
const dfs = (array, sum, index) => { | ||
if (sum > target) return; | ||
if (sum === target) return answer.push(array); | ||
|
||
for (let i = index; i < candidates.length; i++) { | ||
const nextArray = array.concat(candidates[i]); | ||
const nextSum = sum + candidates[i]; | ||
|
||
dfs(nextArray, nextSum, i); | ||
} | ||
}; | ||
|
||
candidates.forEach((value, i) => dfs([value], value, i)); | ||
|
||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* recursive function | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const answer = Array.from({ length: nums.length }, () => 0); | ||
|
||
const search = (left, right, i) => { | ||
if (i === nums.length - 1) { | ||
answer[i] = left * right; | ||
return nums[i]; | ||
} | ||
|
||
const productLeft = left * nums[i]; | ||
const productRight = search(productLeft, right, i + 1); | ||
|
||
answer[i] = left * productRight; | ||
|
||
return productRight * nums[i]; | ||
}; | ||
|
||
search(1, 1, 0); | ||
|
||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. O(n^2) brute force | ||
* 2. hash table | ||
*/ | ||
|
||
/** | ||
* @description brainstorming 1 solve | ||
* time complexity: O(n^2) | ||
* space complexity: O(1) | ||
*/ | ||
var twoSum = function (nums, target) { | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] === target) return [i, j]; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @description brainstorming 2 solve | ||
* time complexity: O(n^2) | ||
* space complexity: O(n) | ||
*/ | ||
var twoSum = function (nums, target) { | ||
const map = new Map(); | ||
|
||
nums.forEach((num, index) => { | ||
if (!map.get(num)) return map.set(num, [index]); | ||
|
||
map.set(num, map.get(num).concat(index)); | ||
}); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const rest = target - nums[i]; | ||
|
||
if (!map.get(rest)) continue; | ||
|
||
const indexList = map.get(rest); | ||
for (let j = 0; j < indexList.length; j++) { | ||
if (i === indexList[j]) continue; | ||
|
||
return [i, indexList[j]]; | ||
} | ||
} | ||
}; |
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.
이런 내용 정말 좋은 것 같네요 ㅎㅎ 저도 작성해봐야겠네요