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
53 changes: 53 additions & 0 deletions linked-list-cycle/Blossssom.ts
Copy link
Contributor

@juhui-jeong juhui-jeong Jan 11, 2026

Choose a reason for hiding this comment

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

Floyd 알고리즘 선택해서 공간 O(1)로 풀이하셨네요! 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 head - linked list node
* @returns - 순환되는 리스트인지?
* @description
* - 풀이 1. - 단순 순회 및 조회로 판단, 결국 visit에 다시 돌아온다면 순환
* - 풀이 2. - 1칸, 2칸 포인터를 나눠 결국 순환이라면 돌고 돌아 만나는 과정을 통한 메모리 O(1)의 풀이
*/

// function hasCycle(head: ListNode | null): boolean {
// const visit = new Set();
// let current = head;

// while (current) {
// if (visit.has(current)) {
// return true;
// }

// visit.add(current);
// current = current.next;
// }

// return false;
// }

function hasCycle(head: ListNode | null): boolean {
if (!head || !head.next) {
return false;
}

let slow: ListNode | null = head;
let fast: ListNode | null = head;

while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;

if (slow === fast) {
return true;
}
}
return false;
}


37 changes: 37 additions & 0 deletions maximum-product-subarray/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param nums - 정수 배열
* @returns - 정수 배열의 부분 배열의 곱이 가장 큰 값을 반환
* @description
* - 값이 음수일 경우 최솟값과 곱해 최댓값을 도출 할 수 있으므로 swap
* - min, max를 현재 값과 비교하며 변경
*/

function maxProduct(nums: number[]): number {
let maximum = nums[0];
let minimum = nums[0];
let result = nums[0];

for (let i = 1; i < nums.length; i++) {
const current = nums[i];

if (current < 0) {
let temp = maximum;
maximum = minimum;
minimum = temp;
}

maximum = Math.max(current, current * maximum);
minimum = Math.min(current, current * minimum);

result = Math.max(result, maximum);
}

return result;
}

const nums = [-2, 3, -4];

maxProduct(nums);



89 changes: 89 additions & 0 deletions pacific-atlantic-water-flow/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @param heights - 셀의 해발고도 배열
* @returns
* @description
* - 인접한 셀의 높이가 작거나 같으면 빗물이 흐를 수 있음
* - 바다에 인접한 셀에서는 바다로 물이 흐를 수 있음
* - 태평양과 대서양 모두로 흐를 수 있는 경로 반환
* - 결국 [0, n] 과 [m, 0]는 반드시 포함
*
* 1. 역순으로 가는게 유리하다는 힌트를 채택
* 2. 모든 셀의 이동 가능 방향을 파악 (dfs, bfs)
*/

function pacificAtlantic(heights: number[][]): number[][] {
const yMax = heights.length;
const xMax = heights[0].length;

// 태평양, 대서양의 visit 체크를 위한 set
const pacific = new Set<string>();
const atlantic = new Set<string>();

const result: number[][] = [];
function dfs(y: number, x: number, visited: Set<string>, prevHeight: number) {
const key = `${y},${x}`;

if (
x < 0 ||
x >= xMax ||
y < 0 ||
y >= yMax ||
visited.has(key) ||
prevHeight > heights[y][x]
) {
return;
}

visited.add(key);
dfs(y, x + 1, visited, heights[y][x]);
dfs(y, x - 1, visited, heights[y][x]);
dfs(y + 1, x, visited, heights[y][x]);
dfs(y - 1, x, visited, heights[y][x]);
}

// 맨 양 끝쪽을 dfs의 시작점으로 지정
for (let i = 0; i < yMax; i++) {
dfs(i, 0, pacific, heights[i][0]);
dfs(i, xMax - 1, atlantic, heights[i][xMax - 1]);
}

for (let j = 0; j < xMax; j++) {
dfs(0, j, pacific, heights[0][j]);
dfs(yMax - 1, j, atlantic, heights[yMax - 1][j]);
}

// 유니온 값 체크
for (let i = 0; i < yMax; i++) {
for (let j = 0; j < xMax; j++) {
const key = `${i},${j}`;

if (pacific.has(key) && atlantic.has(key)) {
result.push([i, j]);
}
}
}

return result;
}

const heights = [
[1, 2, 2, 3, 5],
[3, 2, 3, 4, 4],
[2, 4, 5, 3, 1],
[6, 7, 1, 4, 5],
[5, 1, 1, 2, 4],
];

pacificAtlantic(heights);

// 태 평 양 (Pacific)
// ~ ~ ~ ~ ~ ~
// ~ [1] [2] [2] [3] (5) ~ <-- 오른쪽/아래는 대서양
// ~ [3] [2] [3] (4) (4) ~
// ~ [2] [4] (5) [3] [1] ~
// ~ (6) (7) (1) [4] [5] ~
// ~ (5) [1] [1] [2] [4] ~
// ~ ~ ~ ~ ~ ~
// 대 서 양 (Atlantic)


28 changes: 28 additions & 0 deletions sum-of-two-integers/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
*
* @param a - 정수 a
* @param b - 정수 b
* @returns - 더하기 연산 없이 더한 값 반환
* @description
* - bit 연산이구나! 싶었는데 비트를 잘 다룰줄 몰라서 ai의 도움을 받음
*/

function getSum(a: number, b: number): number {
while (b !== 0) {
// 올림수 계산 : 둘다 1인 비트를 AND 연산으로 찾아 왼쪽으로 밈 (올림수니까)
const carry = (a & b) << 1;

// XOR 연산으로 올림수를 제외한 덧셈 값 도출 (1 | 0 이므로)
a = a ^ b;

b = carry;
}

return a;
}

const a = 2;
const b = 3;
getSum(a, b);