-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from wogha95/main
[์ฌํธ] WEEK 03 Solutions
- Loading branch information
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,21 @@ | ||
// DP ํ์ฉํ์๊ณ ๋ฉ๋ชจ๋ฆฌ ์ถ์๋ฅผ ์ํด ํ์ฌ์ ์ง์ ์ ๊ฒฝ์ฐ์ ์๋ง ๊ด๋ฆฌํ์์ต๋๋ค. | ||
// TC: O(N) | ||
// SC: O(1) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
let previousStep = 0; | ||
let currentStep = 1; | ||
|
||
while (n > 0) { | ||
n -= 1; | ||
const nextStep = previousStep + currentStep; | ||
previousStep = currentStep; | ||
currentStep = nextStep; | ||
} | ||
|
||
return currentStep; | ||
}; |
This file contains 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,23 @@ | ||
// TC: O(C * A) | ||
// SC: O(C) | ||
// C: coins.length, A: amount | ||
|
||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function (coins, amount) { | ||
const dp = new Array(amount + 1).fill(Number.MAX_SAFE_INTEGER); | ||
dp[0] = 0; | ||
|
||
for (let index = 1; index < amount + 1; index++) { | ||
for (const coin of coins) { | ||
if (index - coin >= 0) { | ||
dp[index] = Math.min(dp[index - coin] + 1, dp[index]); | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] === Number.MAX_SAFE_INTEGER ? -1 : dp[amount]; | ||
}; |
This file contains 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,84 @@ | ||
// 2์ฐจ | ||
// ๋ชซ์ ํ์ฉํ์ฌ ์๊ฐ๋ณต์ก๋๋ฅผ ์ค์ด๊ณ ์ ํ์์ผ๋ generateSubResult์์ ์ถ๊ฐ ์๊ฐ๋ฐ์์ผ๋ก ์ ์๋ฏธํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๊ฒฌํ์ง ๋ชปํ์ต๋๋ค. | ||
// TC: O(C^2 * T) | ||
// SC: O(T) | ||
// C: candidates.length, T: target | ||
|
||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
const result = []; | ||
// 'q' means 'quotient'. | ||
const qList = candidates.map((candidate) => Math.floor(target / candidate)); | ||
|
||
dfs([], 0); | ||
|
||
return result; | ||
|
||
function dfs(selectedQList, total) { | ||
if (total > target) { | ||
return; | ||
} | ||
|
||
if (total === target) { | ||
result.push(generateSubResult(selectedQList)); | ||
return; | ||
} | ||
|
||
const currentIndex = selectedQList.length; | ||
for (let q = qList[currentIndex]; q >= 0; q--) { | ||
selectedQList.push(q); | ||
dfs(selectedQList, total + candidates[currentIndex] * q); | ||
selectedQList.pop(); | ||
} | ||
} | ||
|
||
function generateSubResult(selectedQList) { | ||
return selectedQList | ||
.map((q, index) => new Array(q).fill(candidates[index])) | ||
.flat(); | ||
} | ||
}; | ||
|
||
// 1์ฐจ | ||
// dfs๋ฅผ ํ์ฉํ์ฌ ๊ฐ ์์๋ฅผ ์ถ๊ฐ -> ์ฌ๊ท -> ์ ๊ฑฐ -> ์ฌ๊ท๋ก ์ํํฉ๋๋ค. | ||
// TC: O(C^T) | ||
// SC: O(C) | ||
// C: candidates.length, T: target | ||
|
||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
const result = []; | ||
|
||
dfs([], 0, 0); | ||
|
||
return result; | ||
|
||
function dfs(subResult, total, currentIndex) { | ||
if (total > target) { | ||
return; | ||
} | ||
|
||
if (total === target) { | ||
result.push([...subResult]); | ||
return; | ||
} | ||
|
||
if (currentIndex === candidates.length) { | ||
return; | ||
} | ||
|
||
const candidate = candidates[currentIndex]; | ||
subResult.push(candidate); | ||
dfs(subResult, total + candidate, currentIndex); | ||
subResult.pop(); | ||
dfs(subResult, total, currentIndex + 1); | ||
} | ||
}; |
This file contains 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,59 @@ | ||
// ์ข->์ฐ ๋ฐฉํฅ์ ๋์ ๊ณฑ๊ณผ ์ฐ->์ข ๋ฐฉํฅ์ ๋์ ๊ณฑ ํ์ฉ | ||
// TC: O(N) | ||
// SC: O(N) (๋ต์์ ์ ์ธํ๋ฉด O(1)) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const result = new Array(nums.length).fill(1); | ||
|
||
let leftToRight = 1; | ||
for (let index = 1; index < nums.length; index++) { | ||
leftToRight *= nums[index - 1]; | ||
result[index] *= leftToRight; | ||
} | ||
|
||
let rightToLeft = 1; | ||
for (let index = nums.length - 2; index >= 0; index--) { | ||
rightToLeft *= nums[index + 1]; | ||
result[index] *= rightToLeft; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// 1์ฐจ ํ์ด | ||
// 0์ ๊ฐฏ์๊ฐ 0๊ฐ, 1๊ฐ, 2๊ฐ์ธ ๊ฒฝ์ฐ๋ก ๋๋ ์ ํ์ด | ||
// 0๊ฐ์ธ ๊ฒฝ์ฐ, ๋ต์ ๋ฐฐ์ด์ ์์๋ '๋ชจ๋ ์์ ๊ณฑ / ํ์ฌ ์์' | ||
// 1๊ฐ์ธ ๊ฒฝ์ฐ, 0์ ์์นํ ์์๋ง '0์ ์ ์ธํ ๋ชจ๋ ์์ ๊ณฑ' ์ด๊ณ ๊ทธ ์ธ '0' | ||
// 2๊ฐ์ธ ๊ฒฝ์ฐ, ๋ต์ ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ '0' | ||
|
||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const zeroCount = nums.filter((num) => num === 0).length; | ||
if (zeroCount > 1) { | ||
return new Array(nums.length).fill(0); | ||
} | ||
|
||
const multipled = nums.reduce( | ||
(total, current) => (current === 0 ? total : total * current), | ||
1 | ||
); | ||
|
||
if (zeroCount === 1) { | ||
const zeroIndex = nums.findIndex((num) => num === 0); | ||
const result = new Array(nums.length).fill(0); | ||
result[zeroIndex] = multipled; | ||
return result; | ||
} | ||
|
||
return nums.map((num) => multipled / num); | ||
}; |
This file contains 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,51 @@ | ||
// 2์ฐจ: index๋ฅผ ๊ฐ์ผ๋ก ๊ฐ๋ Map์ ํ์ฉํ์ฌ ํ๋ฒ์ ์ํ๋ก ๋ต์ ๋์ถ | ||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
const valueIndexMap = new Map(); | ||
|
||
for (let index = 0; index < nums.length; index++) { | ||
const value = nums[index]; | ||
const anotherValue = target - value; | ||
|
||
if (valueIndexMap.has(anotherValue)) { | ||
return [index, valueIndexMap.get(anotherValue)]; | ||
} | ||
|
||
valueIndexMap.set(value, index); | ||
} | ||
}; | ||
|
||
// 1์ฐจ: ์ ๋ ฌ ํ ํฌํฌ์ธํฐ ํ์ฉ | ||
// TC: O(N * logN) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
const sortedNums = nums | ||
.map((value, index) => ({ value, index })) | ||
.sort((a, b) => a.value - b.value); | ||
let left = 0; | ||
let right = sortedNums.length - 1; | ||
|
||
while (left < right) { | ||
const sum = sortedNums[left].value + sortedNums[right].value; | ||
if (sum < target) { | ||
left += 1; | ||
} else if (sum > target) { | ||
right -= 1; | ||
} else { | ||
return [sortedNums[left].index, sortedNums[right].index]; | ||
} | ||
} | ||
}; |