Skip to content

Commit 1a8b5c6

Browse files
authored
Merge pull request #792 from mmyeon/main
[mallayon] Week 3
2 parents ca1839f + 130e582 commit 1a8b5c6

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

โ€Žcombination-sum/mmyeon.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
3+
* - ์ค‘๋ณต ํฌํ•จํ•˜์—ฌ ๋ชจ๋“  ์กฐํ•ฉ ๊ตฌํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ํ’€๊ธฐ
4+
* - ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” ํƒ€๊ฒŸ ์ค„์—ฌ๊ฐ€๋ฉด์„œ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
5+
* - ๋™์ผ ์กฐํ•ฉ์ถ”๊ฐ€๋˜์ง€ ์•Š๋„๋ก, startIndex ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋„๋ก ์ œํ•œ
6+
*
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^target)
9+
* - candidates ๋ฐฐ์—ด ๊ธธ์ด n๋งŒํผ ์žฌ๊ท€๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , ๊ฐ ํ˜ธ์ถœ์€ target ๊ธธ์ด ๋งŒํผ ์ค‘์ฒฉ๋˜๋‹ˆ๊นŒ O(n^target)
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(target)
12+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ target๋งŒํผ ์žฌ๊ท€ ํ˜ธ์ถœ๋˜๋‹ˆ๊นŒ O(target)
13+
*
14+
*/
15+
16+
function combinationSum(candidates: number[], target: number): number[][] {
17+
const result: number[][] = [];
18+
19+
const dfs = (target: number, combination: number[], startIndex: number) => {
20+
if (target === 0) {
21+
result.push([...combination]);
22+
return;
23+
}
24+
25+
if (target < 0) return;
26+
27+
for (let i = startIndex; i < candidates.length; i++) {
28+
combination.push(candidates[i]);
29+
dfs(target - candidates[i], combination, i);
30+
combination.pop();
31+
}
32+
};
33+
34+
dfs(target, [], 0);
35+
36+
return result;
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
4+
* - O(n)์œผ๋กœ ํ’€์–ด์•ผ ํ•˜๋‹ˆ๊นŒ ์ค‘์ฒฉ์ด ์•„๋‹Œ ๋ฐฐ์—ด ๊ฐœ๋ณ„๋กœ 2๋ฒˆ ์ˆœํšŒ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผ
5+
* - ์™ผ์ชฝ ๊ณฑ(prefixProduct)๊ณผ ์˜ค๋ฅธ์ชฝ ๊ณฑ((suffixProduct)์„ ๋”ฐ๋กœ ๊ณ„์‚ฐํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’์— ์ €์žฅ
6+
*
7+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
8+
* - ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(n)
9+
*
10+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
11+
* - ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅํ•˜๋‹ˆ๊นŒ O(n)
12+
*
13+
*/
14+
15+
function productExceptSelf(nums: number[]): number[] {
16+
let result: number[] = Array(nums.length).fill(1);
17+
let prefixProduct = 1;
18+
let suffixProduct = 1;
19+
20+
for (let i = 0; i < nums.length; i++) {
21+
result[i] = prefixProduct;
22+
prefixProduct *= nums[i];
23+
}
24+
25+
for (let i = nums.length - 1; i >= 0; i--) {
26+
result[i] *= suffixProduct;
27+
suffixProduct *= nums[i];
28+
}
29+
30+
return result;
31+
}

โ€Žtwo-sum/mmyeon.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
4+
* - ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ˆซ์ž์™€ ์ธ๋ฑ์Šค๋ฅผ ๋งต์— ์ €์žฅ
5+
* - nums ๋ฐฐ์—ด ์ˆœํšŒํ•˜๋ฉด์„œ, ์ฐพ๋Š” ์ˆซ์ž ๊ฐ€ ์—†์œผ๋ฉด ๋งต์— ๊ฐ’, ์ธ๋ฑ์Šค ์ถ”๊ฐ€ํ•˜๊ธฐ
6+
* - ๋งต์— ์กด์žฌํ•˜๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค์™€, ํ•ด๋‹น ์ˆซ์ž์˜ ์ธ๋ฑ์Šค ๋‹ด์•„์„œ ์ฆ‰์‹œ ๋ฆฌํ„ดํ•˜๊ธฐ
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
9+
* - nums ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ 1ํšŒ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(n)
10+
* - ๋งต ์กฐํšŒ ๋ฐ ์‚ฝ์ž…์€ O(1)
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
13+
* - ๋งต์— ๋ฐฐ์—ด์˜ ๊ฐ’ ์ €์žฅํ•˜๋‹ˆ๊นŒ O(n)
14+
*
15+
* ์—ฃ์ง€ ์ผ€์ด์Šค :
16+
* - ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ : [3, 3], 6 => [0,1]
17+
*/
18+
19+
function twoSum(nums: number[], target: number): number[] {
20+
const map = new Map<number, number>();
21+
22+
for (let i = 0; i < nums.length; i++) {
23+
const neededValue = target - nums[i];
24+
25+
if (map.has(neededValue)) {
26+
return [map.get(neededValue)!, i];
27+
}
28+
29+
map.set(nums[i], i);
30+
}
31+
}

0 commit comments

Comments
ย (0)