Skip to content

Commit

Permalink
Merge pull request #801 from mike2ox/main
Browse files Browse the repository at this point in the history
[moonhyeok] Week3
  • Loading branch information
mike2ox authored Dec 28, 2024
2 parents 2fec6cf + 97a8b73 commit 834ca1e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
27 changes: 27 additions & 0 deletions combination-sum/mike2ox.ts
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;
}
22 changes: 22 additions & 0 deletions maximum-subarray/mike2ox.ts
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;
}
25 changes: 25 additions & 0 deletions product-of-array-except-self/mike2ox.ts
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;
}
16 changes: 16 additions & 0 deletions reverse-bits/mike2ox.ts
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์œผ๋กœ ๋น„ํŠธ ์ด๋™
}
23 changes: 23 additions & 0 deletions two-sum/mike2ox.ts
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 [];
}

0 comments on commit 834ca1e

Please sign in to comment.