Skip to content

Commit 8cdb2dc

Browse files
authored
Merge pull request #375 from wogha95/main
[재호] WEEK 03 Solutions
2 parents ffade1e + 7788008 commit 8cdb2dc

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

climbing-stairs/wogha95.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// DP 활용하였고 메모리 축소를 위해 현재와 직전의 경우의 수만 관리하였습니다.
2+
// TC: O(N)
3+
// SC: O(1)
4+
5+
/**
6+
* @param {number} n
7+
* @return {number}
8+
*/
9+
var climbStairs = function (n) {
10+
let previousStep = 0;
11+
let currentStep = 1;
12+
13+
while (n > 0) {
14+
n -= 1;
15+
const nextStep = previousStep + currentStep;
16+
previousStep = currentStep;
17+
currentStep = nextStep;
18+
}
19+
20+
return currentStep;
21+
};

coin-change/wogha95.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// TC: O(C * A)
2+
// SC: O(C)
3+
// C: coins.length, A: amount
4+
5+
/**
6+
* @param {number[]} coins
7+
* @param {number} amount
8+
* @return {number}
9+
*/
10+
var coinChange = function (coins, amount) {
11+
const dp = new Array(amount + 1).fill(Number.MAX_SAFE_INTEGER);
12+
dp[0] = 0;
13+
14+
for (let index = 1; index < amount + 1; index++) {
15+
for (const coin of coins) {
16+
if (index - coin >= 0) {
17+
dp[index] = Math.min(dp[index - coin] + 1, dp[index]);
18+
}
19+
}
20+
}
21+
22+
return dp[amount] === Number.MAX_SAFE_INTEGER ? -1 : dp[amount];
23+
};

combination-sum/wogha95.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// 2차
2+
// 몫을 활용하여 시간복잡도를 줄이고자 하였으나 generateSubResult에서 추가 시간발생으로 유의미한 결과를 발견하지 못했습니다.
3+
// TC: O(C^2 * T)
4+
// SC: O(T)
5+
// C: candidates.length, T: target
6+
7+
/**
8+
* @param {number[]} candidates
9+
* @param {number} target
10+
* @return {number[][]}
11+
*/
12+
var combinationSum = function (candidates, target) {
13+
const result = [];
14+
// 'q' means 'quotient'.
15+
const qList = candidates.map((candidate) => Math.floor(target / candidate));
16+
17+
dfs([], 0);
18+
19+
return result;
20+
21+
function dfs(selectedQList, total) {
22+
if (total > target) {
23+
return;
24+
}
25+
26+
if (total === target) {
27+
result.push(generateSubResult(selectedQList));
28+
return;
29+
}
30+
31+
const currentIndex = selectedQList.length;
32+
for (let q = qList[currentIndex]; q >= 0; q--) {
33+
selectedQList.push(q);
34+
dfs(selectedQList, total + candidates[currentIndex] * q);
35+
selectedQList.pop();
36+
}
37+
}
38+
39+
function generateSubResult(selectedQList) {
40+
return selectedQList
41+
.map((q, index) => new Array(q).fill(candidates[index]))
42+
.flat();
43+
}
44+
};
45+
46+
// 1차
47+
// dfs를 활용하여 각 요소를 추가 -> 재귀 -> 제거 -> 재귀로 순회합니다.
48+
// TC: O(C^T)
49+
// SC: O(C)
50+
// C: candidates.length, T: target
51+
52+
/**
53+
* @param {number[]} candidates
54+
* @param {number} target
55+
* @return {number[][]}
56+
*/
57+
var combinationSum = function (candidates, target) {
58+
const result = [];
59+
60+
dfs([], 0, 0);
61+
62+
return result;
63+
64+
function dfs(subResult, total, currentIndex) {
65+
if (total > target) {
66+
return;
67+
}
68+
69+
if (total === target) {
70+
result.push([...subResult]);
71+
return;
72+
}
73+
74+
if (currentIndex === candidates.length) {
75+
return;
76+
}
77+
78+
const candidate = candidates[currentIndex];
79+
subResult.push(candidate);
80+
dfs(subResult, total + candidate, currentIndex);
81+
subResult.pop();
82+
dfs(subResult, total, currentIndex + 1);
83+
}
84+
};
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 좌->우 방향의 누적곱과 우->좌 방향의 누적곱 활용
2+
// TC: O(N)
3+
// SC: O(N) (답안을 제외하면 O(1))
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number[]}
8+
*/
9+
var productExceptSelf = function (nums) {
10+
const result = new Array(nums.length).fill(1);
11+
12+
let leftToRight = 1;
13+
for (let index = 1; index < nums.length; index++) {
14+
leftToRight *= nums[index - 1];
15+
result[index] *= leftToRight;
16+
}
17+
18+
let rightToLeft = 1;
19+
for (let index = nums.length - 2; index >= 0; index--) {
20+
rightToLeft *= nums[index + 1];
21+
result[index] *= rightToLeft;
22+
}
23+
24+
return result;
25+
};
26+
27+
// 1차 풀이
28+
// 0의 갯수가 0개, 1개, 2개인 경우로 나눠서 풀이
29+
// 0개인 경우, 답안 배열의 원소는 '모든 원소 곱 / 현재 원소'
30+
// 1개인 경우, 0의 위치한 원소만 '0을 제외한 모든 원소 곱' 이고 그 외 '0'
31+
// 2개인 경우, 답안 배열의 모든 원소가 '0'
32+
33+
// TC: O(N)
34+
// SC: O(N)
35+
36+
/**
37+
* @param {number[]} nums
38+
* @return {number[]}
39+
*/
40+
var productExceptSelf = function (nums) {
41+
const zeroCount = nums.filter((num) => num === 0).length;
42+
if (zeroCount > 1) {
43+
return new Array(nums.length).fill(0);
44+
}
45+
46+
const multipled = nums.reduce(
47+
(total, current) => (current === 0 ? total : total * current),
48+
1
49+
);
50+
51+
if (zeroCount === 1) {
52+
const zeroIndex = nums.findIndex((num) => num === 0);
53+
const result = new Array(nums.length).fill(0);
54+
result[zeroIndex] = multipled;
55+
return result;
56+
}
57+
58+
return nums.map((num) => multipled / num);
59+
};

two-sum/wogha95.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 2차: index를 값으로 갖는 Map을 활용하여 한번의 순회로 답안 도출
2+
// TC: O(N)
3+
// SC: O(N)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} target
8+
* @return {number[]}
9+
*/
10+
var twoSum = function (nums, target) {
11+
const valueIndexMap = new Map();
12+
13+
for (let index = 0; index < nums.length; index++) {
14+
const value = nums[index];
15+
const anotherValue = target - value;
16+
17+
if (valueIndexMap.has(anotherValue)) {
18+
return [index, valueIndexMap.get(anotherValue)];
19+
}
20+
21+
valueIndexMap.set(value, index);
22+
}
23+
};
24+
25+
// 1차: 정렬 후 투포인터 활용
26+
// TC: O(N * logN)
27+
// SC: O(N)
28+
29+
/**
30+
* @param {number[]} nums
31+
* @param {number} target
32+
* @return {number[]}
33+
*/
34+
var twoSum = function (nums, target) {
35+
const sortedNums = nums
36+
.map((value, index) => ({ value, index }))
37+
.sort((a, b) => a.value - b.value);
38+
let left = 0;
39+
let right = sortedNums.length - 1;
40+
41+
while (left < right) {
42+
const sum = sortedNums[left].value + sortedNums[right].value;
43+
if (sum < target) {
44+
left += 1;
45+
} else if (sum > target) {
46+
right -= 1;
47+
} else {
48+
return [sortedNums[left].index, sortedNums[right].index];
49+
}
50+
}
51+
};

0 commit comments

Comments
 (0)