Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yjason-K committed Feb 10, 2025
1 parent 356e622 commit 88214b4
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
31 changes: 31 additions & 0 deletions find-minimum-in-rotated-sorted-array/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* λ°°μ—΄μ—μ„œ κ°€μž₯ μž‘μ€ 수 μ°ΎκΈ° ( μ œμ•½ : μ‹œκ°„ λ³΅μž‘λ„ O(log n) )
* @param {number[]} nums νšŒμ „λœ 수 λ°°μ—΄
*
* μ‹œκ°„ 볡작되: O(log n)
* - 이뢄 탐색을 μ‚¬μš©ν•˜μ—¬ μ΅œμ†Œκ°’μ„ 탐색
*
* 곡간 λ³΅μž‘λ„: O(1)
*/
function findMin(nums: number[]): number {
let left = 0, right = nums.length - 1;

while (left <= right) {
let mid = Math.floor((left + right) / 2);

// 정렬이 망가진 경우
if (nums[mid] < nums[mid-1]) return nums[mid];

// left, right λ²”μœ„ μ€„μ—¬λ‚˜κ°€κΈ°
if (nums[0] < nums[mid]){
left = mid + 1;
} else {
right = mid - 1;
}
}

// 탐색 후에도 찾지 λͺ»ν•œ 경우 νšŒμ „λ˜μ§€ μ•Šμ€ 경우
return nums[0];

}

37 changes: 37 additions & 0 deletions linked-list-cycle/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

/**
* μ—°κ²° λ¦¬μŠ€νŠΈμΈκ°€ μˆœν™˜ν•˜λŠ”μ§€ μ—¬λΆ€ 확인
* @param {ListNode} head - ListNode
* @returns {boolean} - μˆœν™˜ μ—¬λΆ€
*
* μ‹œκ°„ λ³΅μž‘λ„: O(n)
*
* 곡간 λ³΅μž‘λ„: O(n)
* - λ…Έλ“œμ˜ 개수만큼 Set에 μ €μž₯
*/
function hasCycle(head: ListNode | null): boolean {
const set = new Set();

while (head) {
if (set.has(head)) {
return true;
}

set.add(head);
head = head.next;
}

return false;
}

32 changes: 32 additions & 0 deletions maximum-product-subarray/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* subArray 쀑 μ΅œλŒ€ 곱을 κ΅¬ν•˜λŠ” ν•¨μˆ˜
* @param {number[]} nums - 숫자 λ°°μ—΄
* @returns {number} - subArray 쀑 μ΅œλŒ€ κ³±
*
* @description 음수의 곱이 쑴재 ν•  수 있기 λ•Œλ¬Έμ—, μ΅œλŒ€ κ°’κ³Ό μ΅œμ†Œκ°’μ„ κ°±μ‹ ν•˜λ©°, κ²°κ³Ό 값을 κ°±μ‹ .
*
* μ‹œκ°„ λ³΅μž‘λ„ : O(n)
* - nums λ°°μ—΄ 1회 순회
*
* 곡간 λ³΅μž‘λ„ : O(1)
*/
function maxProduct(nums: number[]): number {
let max = nums[0];
let min = nums[0];
let result = nums[0];

// 첫 번째 μš”μ†Œλ₯Ό μ œμ™Έν•œ λͺ¨λ“  μš”μ†Œλ₯Ό 탐색
for (let i = 1; i < nums.length; i++) {
let current = nums[i]

// ν˜„μž¬ κ°’, 이전 μ΅œλŒ€ 곱과의 κ³±, 이전 μ΅œμ†Œ 곱과의 κ³± 쀑 μ΅œλŒ€/μ΅œμ†Œ κ°±μ‹ 
const cases = [current * max, current * min, current];

max = Math.max(...cases);
min = Math.min(...cases);
result = Math.max(result, max);
}

return result;
}

76 changes: 76 additions & 0 deletions pacific-atlantic-water-flow/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 깊이 μš°μ„  탐색(DFS)을 μ‚¬μš©ν•˜μ—¬ νŠΉμ • λ°”λ‹€μ—μ„œ 올라갈 수 μžˆλŠ” μ’Œν‘œμ„ μ €μž₯
* @param i ν˜„μž¬ μœ„μΉ˜μ˜ ν–‰ (row)
* @param j ν˜„μž¬ μœ„μΉ˜μ˜ μ—΄ (column)
* @param visited λ°©λ¬Έν•œ μ’Œν‘œλ₯Ό μ €μž₯ν•˜λŠ” Set (λ°”λ‹€μ—μ„œ 도달할 수 μžˆλŠ” μœ„μΉ˜λ₯Ό μ €μž₯)
* @param heights 높이 정보가 λ‹΄κΈ΄ 2차원 λ°°μ—΄
*
* μ‹œκ°„ λ³΅μž‘λ„: O(m Γ— n)
* - 각 셀은 μ΅œλŒ€ ν•œ 번 λ°©λ¬Έν•˜λ©°, 총 m Γ— n개의 셀을 탐색함
*
* 곡간 λ³΅μž‘λ„: O(m Γ— n)
* - `visited` Set에 μ΅œλŒ€ m Γ— n개의 μ’Œν‘œλ₯Ό μ €μž₯ κ°€λŠ₯
* - μž¬κ·€ 호좜 μŠ€νƒμ˜ κΉŠμ΄λŠ” O(m + n) (μ΅œμ•…μ˜ 경우 κ°€μž₯ κΈ΄ 경둜λ₯Ό 따라 탐색)
*/
function dfs(i: number, j: number, visited: Set<string>, heights: number[][]) {
if (visited.has(`${i},${j}`)) return;

visited.add(`${i},${j}`);

for (const [di, dj] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) {
const newI = i + di;
const newJ = j + dj;

if (
newI >= 0 && newI < heights.length &&
newJ >= 0 && newJ < heights[0].length &&
heights[newI][newJ] >= heights[i][j]
) {
dfs(newI, newJ, visited, heights);
}
}
};

/**
* 두 λ°”λ‹€ λͺ¨λ‘ 도달할 수 μžˆλŠ” μ’Œν‘œλ₯Ό μ°ΎλŠ” ν•¨μˆ˜
*
* @param heights 2차원 λ°°μ—΄λ‘œ 이루어진 μ§€ν˜•μ˜ 높이 정보
* @returns 두 λ°”λ‹€ λͺ¨λ‘ 도달할 수 μžˆλŠ” μ’Œν‘œ λ°°μ—΄
*
* μ‹œκ°„ λ³΅μž‘λ„: O(m Γ— n)
* - νƒœν‰μ–‘ 및 λŒ€μ„œμ–‘μ—μ„œ 각각 DFS μˆ˜ν–‰ β†’ O(m Γ— n)
* - κ²°κ³Όλ₯Ό μ°ΎλŠ” 이쀑 루프 β†’ O(m Γ— n)
*
* 곡간 λ³΅μž‘λ„: O(m Γ— n)
* - `pacificSet`κ³Ό `atlanticSet`에 μ΅œλŒ€ O(m Γ— n)개의 μ’Œν‘œ μ €μž₯
*
*/
function pacificAtlantic(heights: number[][]): number[][] {
if (!heights || heights.length === 0 || heights[0].length === 0) return [];

const rows = heights.length;
const cols = heights[0].length;

const pacificSet = new Set<string>();
const atlanticSet = new Set<string>();

// νƒœν‰μ–‘(μ™Όμͺ½, μœ„μͺ½)μ—μ„œ μΆœλ°œν•˜λŠ” DFS
for (let i = 0; i < rows; i++) dfs(i, 0, pacificSet, heights);
for (let j = 0; j < cols; j++) dfs(0, j, pacificSet, heights);

// λŒ€μ„œμ–‘(였λ₯Έμͺ½, μ•„λž˜μͺ½)μ—μ„œ μΆœλ°œν•˜λŠ” DFS
for (let i = 0; i < rows; i++) dfs(i, cols - 1, atlanticSet, heights);
for (let j = 0; j < cols; j++) dfs(rows - 1, j, atlanticSet, heights);

const result: number[][] = [];
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (pacificSet.has(`${i},${j}`) && atlanticSet.has(`${i},${j}`)) {
result.push([i, j]);
}
}
}

return result;
};

0 comments on commit 88214b4

Please sign in to comment.