-
-
Notifications
You must be signed in to change notification settings - Fork 195
[highball] week3 solutions #388
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
feaa599
two-sum O(n) 풀이 완료
highballplz 756d0ea
Merge branch 'main' of https://github.com/highballplz/dale-leetcode-s…
highballplz a2d9e6a
product of array except self 완료
highballplz e8cef85
combination-sum 완료
highballplz e666b75
combination-sum 풀이를 11부터 빼서 0되는 path 찾는 알고리즘에서 반대로 바꿈
highballplz 8862575
coin change bfs 풀이
highballplz 1fc517a
coin change dp 풀이
highballplz d238880
climbing-stairs 문제 dp 풀이
highballplz 8dc292b
combination-sum 초기 조건 for문으로 주던 방식에서 단일 라인으로 주는 방식으로 변경
highballplz 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,11 @@ | ||
const climbStairs = function (n) { | ||
const dp = [1, 1]; | ||
|
||
for (let i = 0; i < n - 1; i++) { | ||
const temp = dp[1]; | ||
dp[1] = temp + dp[0]; | ||
dp[0] = temp; | ||
} | ||
|
||
return dp[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,27 @@ | ||
const coinChange = function (coins, target) { | ||
const dp = new Array(target + 1).fill(target + 1); | ||
|
||
dp[0] = 0; | ||
for (let i = 1; i <= target; i++) { | ||
for (let j = 0; j < coins.length; j++) { | ||
if (i >= coins[j]) { | ||
dp[i] = Math.min(dp[i], 1 + dp[i - coins[j]]); | ||
} | ||
} | ||
} | ||
return dp[target] === target + 1 ? -1 : dp[target]; | ||
}; | ||
|
||
console.log(coinChange([1, 2, 5], 11)); //3 | ||
console.log(coinChange([2], 3)); //-1 | ||
console.log(coinChange([1], 0)); //0 | ||
|
||
console.log(coinChange([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 40)); //4 | ||
|
||
console.log(coinChange([186, 419, 83, 408], 6249)); //20 | ||
|
||
console.log( | ||
coinChange([411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422], 9864) | ||
); //24 | ||
|
||
console.log(coinChange([1, 2], 3)); //2 |
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,28 @@ | ||
const combinationSum = function (candidates, target) { | ||
const result = []; | ||
const path = []; | ||
|
||
const dfs = function (candidate, sum) { | ||
if (sum > target) return; | ||
|
||
if (sum !== 0) path.push(candidate); | ||
|
||
if (sum === target) { | ||
result.push([...path]); | ||
path.pop(); | ||
return; | ||
} | ||
|
||
for (let i = 0; i < candidates.length; i++) { | ||
if (candidates[i] >= candidate) dfs(candidates[i], sum + candidates[i]); | ||
} | ||
|
||
path.pop(); | ||
}; | ||
|
||
dfs(0, 0); | ||
|
||
return result; | ||
}; | ||
|
||
console.log(combinationSum([2, 3, 5], 8)); |
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,56 @@ | ||
/* -------------------------------------------------------------------------- */ | ||
/* time complexitiy: O(n), space complexitiy: O(n) */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
// const productExceptSelf = function (nums) { | ||
// const result = []; | ||
// const n = nums.length; | ||
|
||
// if (n < 2) return result; | ||
|
||
// const left = [1]; | ||
// const right = [1]; | ||
|
||
// for (let i = 1; i < n; i++) { | ||
// left[i] = left[i - 1] * nums[i - 1]; | ||
// } | ||
|
||
// for (let i = 1; i < n; i++) { | ||
// right[i] = right[i - 1] * nums[n - i]; | ||
// } | ||
|
||
// for (let i = 0; i < n; i++) { | ||
// result[i] = left[i] * right[n - 1 - i]; | ||
// if (result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들 | ||
// } | ||
|
||
// return result; | ||
// }; | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* time complexitiy: O(n), space complexitiy: O(1) */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
const productExceptSelf = function (nums) { | ||
const result = [1]; | ||
const n = nums.length; | ||
|
||
if (n < 2) return result; | ||
|
||
let prefix = 1; | ||
let postfix = 1; | ||
|
||
for (let i = 1; i < n; i++) { | ||
prefix *= nums[i - 1]; | ||
result[i] = prefix; | ||
if (i === n - 1 && result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들 | ||
} | ||
|
||
for (let i = n - 2; i >= 0; i--) { | ||
postfix *= nums[i + 1]; | ||
result[i] *= postfix; | ||
if (result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들 | ||
} | ||
|
||
return result; | ||
}; |
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,44 @@ | ||
const twoSum = function (nums, target) { | ||
/* -------------------------------------------------------------------------- */ | ||
/* target/2인 element가 2개 있을 경우 early return */ | ||
/* -------------------------------------------------------------------------- */ | ||
let indices = []; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] === target / 2) indices.push(i); | ||
if (indices.length === 2) break; | ||
} | ||
|
||
if (indices.length === 2) return indices; | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* absDNums[i] === absDNums[j]인 경우 */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
indices = []; //indices 초기화 | ||
|
||
const dNums = nums.map((num) => num - target / 2); //nums의 각 값을 target/2 값과의 편차들로 변환 | ||
const absDNums = dNums.map(Math.abs); //dNumbs의 각 값에 절댓값 취해주기 | ||
|
||
const uniqueAbsDNums = new Map(); //uniqueAbsDNums은 absDNums의 각 값을 key 값으로 하고 그 key의 부호를 boolean으로 변환한 값을 value로 갖는 map임 | ||
let j; | ||
for (let i = 0; i < absDNums.length; i++) { | ||
if ( | ||
uniqueAbsDNums.has(absDNums[i]) && //앞에 나온 수들 중 하나와 절댓값이 같은 수 등장 (absDNums[i]) | ||
dNums[i] > 0 != uniqueAbsDNums.get(absDNums[i]) //절댓값이 같은 이유가 앞에 나온 수들 중 하나와 아예 같은 값이어서가 아니라 부호만 반대인 수이기 때문이라는 것 체크 (두 수가 target/2로 같았다면 이 조건으로 걸러져 버리기 때문에 앞에서 early return하길 잘했음) | ||
) { | ||
j = i; | ||
break; | ||
} | ||
uniqueAbsDNums.set(absDNums[i], dNums[i] > 0); | ||
} | ||
|
||
for (let i = 0; i < absDNums.length; i++) { | ||
// j index 앞에 나오는, absDNums[j]와 같은 절댓값을 가지는 수 찾기(위 for문에서 j를 찾은 방식 덕분에 이 수는 absDNums[j]와 절댓값은 같지만 부호는 반대인 수일 것임) | ||
if (absDNums[i] === absDNums[j]) { | ||
indices.push(i); | ||
indices.push(j); | ||
break; | ||
} | ||
} | ||
return indices; | ||
}; |
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.