-
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 #801 from mike2ox/main
[moonhyeok] Week3
- Loading branch information
Showing
5 changed files
with
113 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,27 @@ | ||
/** | ||
* source: https://leetcode.com/problems/combination-sum/ | ||
* ํ์ด๋ฐฉ๋ฒ: ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ ์กฐํฉ์ ํ์ | ||
* | ||
* ์๊ฐ๋ณต์ก๋: O(n^m) (n: candidates์ ๊ธธ์ด, m: target์ ๋ง๋ค๊ธฐ ์ํ ์ต๋ ๋ฐ๋ณต ํ์) | ||
* ๊ณต๊ฐ๋ณต์ก๋: O(n^m) (n: candidates์ ๊ธธ์ด, m: target์ ๋ง๋ค๊ธฐ ์ํ ์ต๋ ๋ฐ๋ณต ํ์) | ||
* | ||
* Note | ||
* - ๋น์ฅ์ ๊ตฌํํ๋ ค๋ค๋ณด๋ ์ฌ๊ท๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํ. => ์ถํ ๋ฆฌํฉํ ๋ง ํ์ | ||
*/ | ||
function combinationSum(candidates: number[], target: number): number[][] { | ||
if (target === 0) return [[]]; | ||
if (target < 0) return []; | ||
|
||
const result: number[][] = []; | ||
|
||
for (let i = 0; i < candidates.length; i++) { | ||
const num = candidates[i]; | ||
const subCombos = combinationSum(candidates.slice(i), target - num); | ||
|
||
for (const combo of subCombos) { | ||
result.push([num, ...combo]); | ||
} | ||
} | ||
|
||
return result; | ||
} |
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,22 @@ | ||
/** | ||
* source: https://leetcode.com/problems/maximum-subarray/ | ||
* ํ์ด๋ฐฉ๋ฒ: ํ์ฌ ์์น๊น์ง์ ์ต๋ ํฉ์ ์ ์ฅํ๋ฉด์ ์ ์ฒด ์ต๋ ํฉ์ ๊ฐฑ์ | ||
* ์๊ฐ๋ณต์ก๋: O(n) (n: nums์ ๊ธธ์ด) | ||
* ๊ณต๊ฐ๋ณต์ก๋: O(1) (์์ ๊ณต๊ฐ๋ง ์ฌ์ฉ) | ||
*/ | ||
function maxSubArray(nums: number[]): number { | ||
// ๋ฐฐ์ด์ด ๋น์ด์๋ ๊ฒฝ์ฐ | ||
if (nums.length === 0) return 0; | ||
|
||
let result = nums[0]; // ์ ์ฒด ์ต๋ ํฉ(์ด๊ธฐ๊ฐ์ ์ฒซ ๋ฒ์งธ ์์) | ||
let current = nums[0]; // ํ์ฌ ์์น๊น์ง์ ์ต๋ ํฉ | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
// ํ์ฌ ์์๋ฅผ ๋ํ ๊ฐ๊ณผ ํ์ฌ ์์ ์ค ํฐ ๊ฐ์ ์ ํ | ||
current = Math.max(nums[i], current + nums[i]); | ||
// ์ ์ฒด ์ต๋ ํฉ ๊ฐฑ์ | ||
result = Math.max(result, current); | ||
} | ||
|
||
return result; | ||
} |
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,25 @@ | ||
/** | ||
* source: https://leetcode.com/problems/product-of-array-except-self/ | ||
* ํ์ด๋ฐฉ๋ฒ: ์ผ์ชฝ๋ถํฐ์ ๋์ ๊ณฑ๊ณผ ์ค๋ฅธ์ชฝ๋ถํฐ์ ๋์ ๊ณฑ์ ์ด์ฉํ์ฌ ๊ฒฐ๊ณผ ๊ณ์ฐ | ||
* ์๊ฐ๋ณต์ก๋: O(n) (n: nums์ ๊ธธ์ด) | ||
* ๊ณต๊ฐ๋ณต์ก๋: O(1) (์์ ๊ณต๊ฐ๋ง ์ฌ์ฉ) | ||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const n = nums.length; | ||
const result = new Array(n); | ||
|
||
// ์ผ์ชฝ๋ถํฐ์ ๋์ ๊ณฑ ๊ณ์ฐ | ||
result[0] = 1; | ||
for (let i = 1; i < n; i++) { | ||
result[i] = result[i - 1] * nums[i - 1]; | ||
} | ||
|
||
// ์ค๋ฅธ์ชฝ๋ถํฐ์ ๋์ ๊ณฑ์ ๊ณฑํ๋ฉด์ ๊ฒฐ๊ณผ ๊ณ์ฐ | ||
let right = 1; | ||
for (let i = n - 1; i >= 0; i--) { | ||
result[i] = result[i] * right; | ||
right *= nums[i]; | ||
} | ||
|
||
return result; | ||
} |
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,16 @@ | ||
/** | ||
* source: https://leetcode.com/problems/reverse-bits/ | ||
* ํ์ด๋ฐฉ๋ฒ: ๋นํธ ์ฐ์ฐ์ ์ด์ฉํ์ฌ ๋ค์ง๊ธฐ | ||
* ์๊ฐ๋ณต์ก๋: O(1) | ||
* ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
*/ | ||
function reverseBits(n: number): number { | ||
let result = 0; | ||
const bitSize = 32; | ||
// 32๋นํธ๋ฅผ ์ํํ๋ฉด์ ๋ค์ง๊ธฐ | ||
for (let i = 0; i < bitSize; i++) { | ||
const bit = (n >> i) & 1; // i๋ฒ์งธ ๋นํธ ์ถ์ถ | ||
result = result | (bit << (bitSize - 1 - i)); // ๋ค์ง์ ๋นํธ๋ฅผ result์ ์ ์ฅ | ||
} | ||
return result >>> 0; // ๋ถํธ๋นํธ๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํด 0์ผ๋ก ๋นํธ ์ด๋ | ||
} |
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 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/two-sum/ | ||
* ํ์ด๋ฐฉ๋ฒ: Map์ ์ด์ฉํ์ฌ ํ์ํ ๋๋จธ์ง ์ซ์๋ฅผ ์ ์ฅํ๋ฉด์ ํ์ธ | ||
* ์๊ฐ๋ณต์ก๋: O(n) | ||
* ๊ณต๊ฐ๋ณต์ก๋: O(n) | ||
*/ | ||
function twoSum(nums: number[], target: number): number[] { | ||
// nums์ ๊ฐ์ key๋ก, ์ธ๋ฑ์ค๋ฅผ value๋ก ์ ์ฅํ๋ Map | ||
const numMap = new Map<number, number>(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const remain = target - nums[i]; // ํ์ํ ๋๋จธ์ง ์ซ์ ๊ณ์ฐ | ||
|
||
// ํ์ํ ๋๋จธ์ง ์ซ์๊ฐ Map์ ์๋์ง ์ฒดํฌ | ||
if (numMap.has(remain)) { | ||
return [numMap.get(remain)!, i]; | ||
} | ||
// ํ์ฌ ์ซ์์ ์ธ๋ฑ์ค ์ ์ฅ | ||
numMap.set(nums[i], i); | ||
} | ||
|
||
return []; | ||
} |