Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
42 changes: 11 additions & 31 deletions climbing-stairs/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 피보나치 수열을 이용한 문제로, 중복 계산을 방지하기 위해 메모이제이션 기법을 사용하는 동적 프로그래밍 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
/**
* 문제 유형
* - DP (피보나치)
*
* 문제 설명
* - 계단을 올라가는 방법의 수를 구하기
*
* 아이디어
* 1) 피보나치 수열 활용
* - climbStairs(n) = climbStairs(n-1) + climbStairs(n-2)
*/
function climbStairsBottomUp(n: number): number {
function fibonacci(n: number, memo = new Map<number, number>()) {
if (n === 1) return 1;
if (n === 2) return 2;
function climbStairs(n: number): number {
const memo = new Map<number, number>();

function dp (n: number): number {
if(n <= 1) return 1;
if(memo.has(n)) return memo.get(n)!;

if (memo.has(n)) return memo.get(n);
const result = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
memo.set(n, result);
return result;
const result = dp(n-1) + dp(n-2);
memo.set(n, result);
return result;
}
return fibonacci(n);
}

function climbStairsTopDown(n: number): number {
const dp = new Array(n + 1).fill(0);
dp[1] = 1;
dp[2] = 2;

for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
return dp(n);
};
14 changes: 0 additions & 14 deletions climbing-stairs/soobing3.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions contains-duplicate/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 중복 체크를 위해 해시 맵을 사용하여 각 숫자의 존재 여부를 빠르게 확인하는 방식으로 구현되어 있습니다.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
function containsDuplicate(nums: number[]): boolean {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.get(nums[i])) {
return true;
} else map.set(nums[i], 1);
const checkMap = new Map();
for(const n of nums) {
if(checkMap.has(n)) {
return true;
} else {
checkMap.set(n, 1);
}
}

return false;
}
};
11 changes: 0 additions & 11 deletions contains-duplicate/soobing3.ts

This file was deleted.

File renamed without changes.
42 changes: 9 additions & 33 deletions house-robber/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 이전 계산 결과를 활용하여 최적의 해를 찾는 DP 패턴을 사용하며, 최적 부분 구조를 기반으로 문제를 해결합니다.

Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
/**
* 문제 설명
* - 주어진 배열의 시작과 끝이 연결되어 있는 원형이라고 할 경우에, 이웃하는 집은 털 수 없고 최대로 털 수 있는 돈을 구하는 문제
*
* 아이디어
* 1) 동적 계획법(Dynamic Programming) 활용
* - 원형 구조이므로 첫번째 집과 마지막집은 동시에 털 수 없음
* - 따라서 두 가지 경우로 나누어 계산하고, 두 결과 중 더 큰 값을 반환
* 2) 선형 배열에 대한 최대 금액 계산
* - 매 순회마다 현재 집을 털 경우(prev2 + num)와 털지 않을 경우(prev1) 중 큰 값을 선택
* - 최종적으로 prev1에 최대값이 저장
*/
function robLinear(nums: number[]) {
let prev1 = 0;
let prev2 = 0;

for (const num of nums) {
const temp = prev1;
prev1 = Math.max(prev2 + num, prev1);
prev2 = temp;
}

return prev1;
}

function rob(nums: number[]): number {
if (nums.length === 1) return nums[0];
if (nums.length === 2) return Math.max(nums[0], nums[1]);

const temp1 = robLinear(nums.slice(0, nums.length - 1));
const temp2 = robLinear(nums.slice(1));

return Math.max(temp1, temp2);
}
const dp = new Array(nums.length);
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

for(let i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
}
return dp[nums.length - 1];
};
10 changes: 0 additions & 10 deletions house-robber/soobing3.ts

This file was deleted.

16 changes: 9 additions & 7 deletions maximum-subarray/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 연속된 부분 배열의 최대 합을 찾기 위해 이전 상태를 활용하는 DP 방식을 사용합니다. 최적 부분 구조를 이용한 대표적인 DP 문제입니다.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function maxSubArray(nums: number[]): number {
let currentSum = nums[0];
let maxSum = nums[0];
for (let i = 1; i < nums.length; i++) {
currentSum = Math.max(nums[i], currentSum + nums[i]);
maxSum = Math.max(maxSum, currentSum);
let max = nums[0];
const dp = new Array(nums.length).fill(0);
dp[0] = Math.max(0, nums[0]);

for(let i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
max = Math.max(dp[i], max);
}
return maxSum;
}
return max;
};
11 changes: 0 additions & 11 deletions maximum-subarray/soobing3.ts

This file was deleted.

File renamed without changes.
92 changes: 5 additions & 87 deletions number-of-islands/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 이 코드는 비트 연산자를 사용하여 이진수의 1의 개수를 세는 방식으로, 비트 조작을 활용하는 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -1,91 +1,9 @@
/**
* 문제 설명
* - 2차원 그리드에서 섬의 갯수를 구하는 문제
*
* 조건
* - 가로, 세로가 1로 인접해있는 경우 같은 섬으로 간주
*
* 아이디어
* - 경로 탐색, 네트워크, 조합 유형이 나오면 '그래프 탐색 알고리즘'을 떠울린다.
* 1) DFS (재귀)
* 2) BFS (링크드리스트 or 큐)
*
*/

function numIslands(grid: string[][]): number {
const rows = grid.length;
const cols = grid[0].length;
function hammingWeight(n: number): number {
let count = 0;
function dfs(r: number, c: number) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === "0") return;

grid[r][c] = "0";
dfs(r - 1, c);
dfs(r + 1, c);
dfs(r, c - 1);
dfs(r, c + 1);
}

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === "1") {
count++;
dfs(r, c);
}
}
}
return count;
}

/**
*
* BFS version

function numIslands(grid: string[][]): number {
const rows = grid.length;
const cols = grid[0].length;
let count = 0;

const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
function bfs(r: number, c: number) {
const queue = [[r, c]];
grid[r][c] = "0";

while (queue.length) {
const [row, col] = queue.shift()!;

for (const [dr, dc] of directions) {
const newRow = row + dr;
const newCol = col + dc;

if (
newRow >= 0 &&
newRow < rows &&
newCol >= 0 &&
newCol < cols &&
grid[newRow][newCol] === "1"
) {
queue.push([newRow, newCol]);
grid[newRow][newCol] = "0";
}
}
}
}

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === "1") {
count++;
bfs(r, c);
for(let i = 31; i >= 0; i--) {
if((n >> i) & 1) {
count++;
}
}
}
return count;
}

*/
};
9 changes: 0 additions & 9 deletions number-of-islands/soobing3.ts

This file was deleted.

23 changes: 12 additions & 11 deletions product-of-array-except-self/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Prefix Sum / Cumulative Product
  • 설명: 이 코드는 왼쪽과 오른쪽 누적 곱을 계산하여 각 위치의 곱을 구하는 방식으로, 특정 패턴 목록에 속하지 않지만 누적 계산과 관련된 패턴입니다.

Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
function productExceptSelf(nums: number[]): number[] {
const left = Array(nums.length).fill(1);
const right = Array(nums.length).fill(1);
const result = Array(nums.length);
const n = nums.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
const result = new Array(n).fill(1);

for (let i = 1; i < nums.length; i++) {
left[i] = left[i - 1] * nums[i - 1];
for (let i = 1; i < n; i++) {
left[i] = nums[i-1] * left[i - 1];
}

for (let i = nums.length - 2; i >= 0; i--) {
right[i] = right[i + 1] * nums[i + 1];
for (let i = n - 2; i >= 0; i--) {
right[i] = nums[i+1] * right[i + 1];
}

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

return result;
}
};
20 changes: 0 additions & 20 deletions product-of-array-except-self/soobing3.ts

This file was deleted.

File renamed without changes.
42 changes: 22 additions & 20 deletions two-sum/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 정렬된 배열에서 두 포인터를 이용해 합이 target이 되는 두 수를 찾는 방식으로, Two Pointers 패턴에 속합니다. 포인터를 좌우에서 시작하여 조건에 따라 이동시키는 구조입니다.

Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// 1. Brute force
function twoSum(nums: number[], target: number): number[] {
for (let i = 0; i < nums.length - 1; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
const map = new Map<number, number[]>();
for(let i = 0; i < nums.length; i++) {
const current = map.get(nums[i]);
if(current) {
current.push(i);
} else {
map.set(nums[i], [i]);
}
}
}
return [];
}

// 2. Hashmap
function twoSum(nums: number[], target: number): number[] {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i);
}
nums.sort((a, b) => a - b);
let left = 0;
let right = nums.length - 1;

for (let i = 0; i < nums.length; i++) {
const targetIndex = map.get(target - nums[i]);
if (targetIndex && targetIndex !== i) {
return [i, targetIndex];
}
while(left < right) {
if(nums[left] + nums[right] === target) {
return [map.get(nums[left])?.pop() ?? 0, map.get(nums[right])?.pop() ?? 0];
}

if(nums[left] + nums[right] > target) {
right--;
} else {
left++;
}
}

return [];
}
};
Loading
Loading