Skip to content

Commit

Permalink
Merge pull request #792 from mmyeon/main
Browse files Browse the repository at this point in the history
[mallayon] Week 3
  • Loading branch information
mmyeon authored Dec 28, 2024
2 parents ca1839f + 130e582 commit 1a8b5c6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
37 changes: 37 additions & 0 deletions combination-sum/mmyeon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
* - ์ค‘๋ณต ํฌํ•จํ•˜์—ฌ ๋ชจ๋“  ์กฐํ•ฉ ๊ตฌํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ํ’€๊ธฐ
* - ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” ํƒ€๊ฒŸ ์ค„์—ฌ๊ฐ€๋ฉด์„œ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
* - ๋™์ผ ์กฐํ•ฉ์ถ”๊ฐ€๋˜์ง€ ์•Š๋„๋ก, startIndex ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋„๋ก ์ œํ•œ
*
*
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^target)
* - candidates ๋ฐฐ์—ด ๊ธธ์ด n๋งŒํผ ์žฌ๊ท€๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , ๊ฐ ํ˜ธ์ถœ์€ target ๊ธธ์ด ๋งŒํผ ์ค‘์ฒฉ๋˜๋‹ˆ๊นŒ O(n^target)
*
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(target)
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ target๋งŒํผ ์žฌ๊ท€ ํ˜ธ์ถœ๋˜๋‹ˆ๊นŒ O(target)
*
*/

function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];

const dfs = (target: number, combination: number[], startIndex: number) => {
if (target === 0) {
result.push([...combination]);
return;
}

if (target < 0) return;

for (let i = startIndex; i < candidates.length; i++) {
combination.push(candidates[i]);
dfs(target - candidates[i], combination, i);
combination.pop();
}
};

dfs(target, [], 0);

return result;
}
31 changes: 31 additions & 0 deletions product-of-array-except-self/mmyeon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
*
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
* - O(n)์œผ๋กœ ํ’€์–ด์•ผ ํ•˜๋‹ˆ๊นŒ ์ค‘์ฒฉ์ด ์•„๋‹Œ ๋ฐฐ์—ด ๊ฐœ๋ณ„๋กœ 2๋ฒˆ ์ˆœํšŒ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผ
* - ์™ผ์ชฝ ๊ณฑ(prefixProduct)๊ณผ ์˜ค๋ฅธ์ชฝ ๊ณฑ((suffixProduct)์„ ๋”ฐ๋กœ ๊ณ„์‚ฐํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’์— ์ €์žฅ
*
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
* - ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(n)
*
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
* - ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅํ•˜๋‹ˆ๊นŒ O(n)
*
*/

function productExceptSelf(nums: number[]): number[] {
let result: number[] = Array(nums.length).fill(1);
let prefixProduct = 1;
let suffixProduct = 1;

for (let i = 0; i < nums.length; i++) {
result[i] = prefixProduct;
prefixProduct *= nums[i];
}

for (let i = nums.length - 1; i >= 0; i--) {
result[i] *= suffixProduct;
suffixProduct *= nums[i];
}

return result;
}
31 changes: 31 additions & 0 deletions two-sum/mmyeon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
*
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
* - ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ˆซ์ž์™€ ์ธ๋ฑ์Šค๋ฅผ ๋งต์— ์ €์žฅ
* - nums ๋ฐฐ์—ด ์ˆœํšŒํ•˜๋ฉด์„œ, ์ฐพ๋Š” ์ˆซ์ž ๊ฐ€ ์—†์œผ๋ฉด ๋งต์— ๊ฐ’, ์ธ๋ฑ์Šค ์ถ”๊ฐ€ํ•˜๊ธฐ
* - ๋งต์— ์กด์žฌํ•˜๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค์™€, ํ•ด๋‹น ์ˆซ์ž์˜ ์ธ๋ฑ์Šค ๋‹ด์•„์„œ ์ฆ‰์‹œ ๋ฆฌํ„ดํ•˜๊ธฐ
*
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
* - nums ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ 1ํšŒ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(n)
* - ๋งต ์กฐํšŒ ๋ฐ ์‚ฝ์ž…์€ O(1)
*
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
* - ๋งต์— ๋ฐฐ์—ด์˜ ๊ฐ’ ์ €์žฅํ•˜๋‹ˆ๊นŒ O(n)
*
* ์—ฃ์ง€ ์ผ€์ด์Šค :
* - ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ : [3, 3], 6 => [0,1]
*/

function twoSum(nums: number[], target: number): number[] {
const map = new Map<number, number>();

for (let i = 0; i < nums.length; i++) {
const neededValue = target - nums[i];

if (map.has(neededValue)) {
return [map.get(neededValue)!, i];
}

map.set(nums[i], i);
}
}

0 comments on commit 1a8b5c6

Please sign in to comment.