Skip to content

Commit b37dc9d

Browse files
authored
Merge pull request #388 from highballplz/main
[highball] week3 solutions
2 parents 71419dd + 8dc292b commit b37dc9d

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const climbStairs = function (n) {
2+
const dp = [1, 1];
3+
4+
for (let i = 0; i < n - 1; i++) {
5+
const temp = dp[1];
6+
dp[1] = temp + dp[0];
7+
dp[0] = temp;
8+
}
9+
10+
return dp[1];
11+
};

โ€Žcoin-change/highball.jsโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const coinChange = function (coins, target) {
2+
const dp = new Array(target + 1).fill(target + 1);
3+
4+
dp[0] = 0;
5+
for (let i = 1; i <= target; i++) {
6+
for (let j = 0; j < coins.length; j++) {
7+
if (i >= coins[j]) {
8+
dp[i] = Math.min(dp[i], 1 + dp[i - coins[j]]);
9+
}
10+
}
11+
}
12+
return dp[target] === target + 1 ? -1 : dp[target];
13+
};
14+
15+
console.log(coinChange([1, 2, 5], 11)); //3
16+
console.log(coinChange([2], 3)); //-1
17+
console.log(coinChange([1], 0)); //0
18+
19+
console.log(coinChange([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 40)); //4
20+
21+
console.log(coinChange([186, 419, 83, 408], 6249)); //20
22+
23+
console.log(
24+
coinChange([411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422], 9864)
25+
); //24
26+
27+
console.log(coinChange([1, 2], 3)); //2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const combinationSum = function (candidates, target) {
2+
const result = [];
3+
const path = [];
4+
5+
const dfs = function (candidate, sum) {
6+
if (sum > target) return;
7+
8+
if (sum !== 0) path.push(candidate);
9+
10+
if (sum === target) {
11+
result.push([...path]);
12+
path.pop();
13+
return;
14+
}
15+
16+
for (let i = 0; i < candidates.length; i++) {
17+
if (candidates[i] >= candidate) dfs(candidates[i], sum + candidates[i]);
18+
}
19+
20+
path.pop();
21+
};
22+
23+
dfs(0, 0);
24+
25+
return result;
26+
};
27+
28+
console.log(combinationSum([2, 3, 5], 8));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* time complexitiy: O(n), space complexitiy: O(n) */
3+
/* -------------------------------------------------------------------------- */
4+
5+
// const productExceptSelf = function (nums) {
6+
// const result = [];
7+
// const n = nums.length;
8+
9+
// if (n < 2) return result;
10+
11+
// const left = [1];
12+
// const right = [1];
13+
14+
// for (let i = 1; i < n; i++) {
15+
// left[i] = left[i - 1] * nums[i - 1];
16+
// }
17+
18+
// for (let i = 1; i < n; i++) {
19+
// right[i] = right[i - 1] * nums[n - i];
20+
// }
21+
22+
// for (let i = 0; i < n; i++) {
23+
// result[i] = left[i] * right[n - 1 - i];
24+
// if (result[i] === 0) result[i] = Math.abs(result[i]); //0์ด -0์œผ๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด์Šˆ ํ•ธ๋“ค
25+
// }
26+
27+
// return result;
28+
// };
29+
30+
/* -------------------------------------------------------------------------- */
31+
/* time complexitiy: O(n), space complexitiy: O(1) */
32+
/* -------------------------------------------------------------------------- */
33+
34+
const productExceptSelf = function (nums) {
35+
const result = [1];
36+
const n = nums.length;
37+
38+
if (n < 2) return result;
39+
40+
let prefix = 1;
41+
let postfix = 1;
42+
43+
for (let i = 1; i < n; i++) {
44+
prefix *= nums[i - 1];
45+
result[i] = prefix;
46+
if (i === n - 1 && result[i] === 0) result[i] = Math.abs(result[i]); //0์ด -0์œผ๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด์Šˆ ํ•ธ๋“ค
47+
}
48+
49+
for (let i = n - 2; i >= 0; i--) {
50+
postfix *= nums[i + 1];
51+
result[i] *= postfix;
52+
if (result[i] === 0) result[i] = Math.abs(result[i]); //0์ด -0์œผ๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด์Šˆ ํ•ธ๋“ค
53+
}
54+
55+
return result;
56+
};

โ€Žtwo-sum/highball.jsโ€Ž

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const twoSum = function (nums, target) {
2+
/* -------------------------------------------------------------------------- */
3+
/* target/2์ธ element๊ฐ€ 2๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ early return */
4+
/* -------------------------------------------------------------------------- */
5+
let indices = [];
6+
for (let i = 0; i < nums.length; i++) {
7+
if (nums[i] === target / 2) indices.push(i);
8+
if (indices.length === 2) break;
9+
}
10+
11+
if (indices.length === 2) return indices;
12+
13+
/* -------------------------------------------------------------------------- */
14+
/* absDNums[i] === absDNums[j]์ธ ๊ฒฝ์šฐ */
15+
/* -------------------------------------------------------------------------- */
16+
17+
indices = []; //indices ์ดˆ๊ธฐํ™”
18+
19+
const dNums = nums.map((num) => num - target / 2); //nums์˜ ๊ฐ ๊ฐ’์„ target/2 ๊ฐ’๊ณผ์˜ ํŽธ์ฐจ๋“ค๋กœ ๋ณ€ํ™˜
20+
const absDNums = dNums.map(Math.abs); //dNumbs์˜ ๊ฐ ๊ฐ’์— ์ ˆ๋Œ“๊ฐ’ ์ทจํ•ด์ฃผ๊ธฐ
21+
22+
const uniqueAbsDNums = new Map(); //uniqueAbsDNums์€ absDNums์˜ ๊ฐ ๊ฐ’์„ key ๊ฐ’์œผ๋กœ ํ•˜๊ณ  ๊ทธ key์˜ ๋ถ€ํ˜ธ๋ฅผ boolean์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ value๋กœ ๊ฐ–๋Š” map์ž„
23+
let j;
24+
for (let i = 0; i < absDNums.length; i++) {
25+
if (
26+
uniqueAbsDNums.has(absDNums[i]) && //์•ž์— ๋‚˜์˜จ ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜์™€ ์ ˆ๋Œ“๊ฐ’์ด ๊ฐ™์€ ์ˆ˜ ๋“ฑ์žฅ (absDNums[i])
27+
dNums[i] > 0 != uniqueAbsDNums.get(absDNums[i]) //์ ˆ๋Œ“๊ฐ’์ด ๊ฐ™์€ ์ด์œ ๊ฐ€ ์•ž์— ๋‚˜์˜จ ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜์™€ ์•„์˜ˆ ๊ฐ™์€ ๊ฐ’์ด์–ด์„œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ถ€ํ˜ธ๋งŒ ๋ฐ˜๋Œ€์ธ ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๋Š” ๊ฒƒ ์ฒดํฌ (๋‘ ์ˆ˜๊ฐ€ target/2๋กœ ๊ฐ™์•˜๋‹ค๋ฉด ์ด ์กฐ๊ฑด์œผ๋กœ ๊ฑธ๋Ÿฌ์ ธ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์•ž์—์„œ early returnํ•˜๊ธธ ์ž˜ํ–ˆ์Œ)
28+
) {
29+
j = i;
30+
break;
31+
}
32+
uniqueAbsDNums.set(absDNums[i], dNums[i] > 0);
33+
}
34+
35+
for (let i = 0; i < absDNums.length; i++) {
36+
// j index ์•ž์— ๋‚˜์˜ค๋Š”, absDNums[j]์™€ ๊ฐ™์€ ์ ˆ๋Œ“๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ˆ˜ ์ฐพ๊ธฐ(์œ„ for๋ฌธ์—์„œ j๋ฅผ ์ฐพ์€ ๋ฐฉ์‹ ๋•๋ถ„์— ์ด ์ˆ˜๋Š” absDNums[j]์™€ ์ ˆ๋Œ“๊ฐ’์€ ๊ฐ™์ง€๋งŒ ๋ถ€ํ˜ธ๋Š” ๋ฐ˜๋Œ€์ธ ์ˆ˜์ผ ๊ฒƒ์ž„)
37+
if (absDNums[i] === absDNums[j]) {
38+
indices.push(i);
39+
indices.push(j);
40+
break;
41+
}
42+
}
43+
return indices;
44+
};

0 commit comments

Comments
ย (0)