From 6ecb44da9eb7404275f9d8a0b02b15bf2f979e51 Mon Sep 17 00:00:00 2001 From: chae Date: Mon, 20 Jan 2025 18:21:29 +0900 Subject: [PATCH 1/2] feat: reverse-linked-list solution --- reverse-linked-list/YeomChaeeun.ts | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 reverse-linked-list/YeomChaeeun.ts diff --git a/reverse-linked-list/YeomChaeeun.ts b/reverse-linked-list/YeomChaeeun.ts new file mode 100644 index 000000000..0a4c6ae42 --- /dev/null +++ b/reverse-linked-list/YeomChaeeun.ts @@ -0,0 +1,32 @@ +/** + * 연결 리스트를 뒤집는 알고리즘 + * 알고리즘 복잡도 + * - 시간 복잡도: O(n) + * - 공간 복잡도: O(n) + */ +/** + * 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) + * } + * } + */ +function reverseList(head: ListNode | null): ListNode | null { + // console.log(head) + if (head === null || head.next === null) { + return head + } + + // 마지막 노드에 도달할 때까지 계속 재귀 호출 + const newHead = reverseList(head.next) + + // 백트래킹 과정 + head.next.next = head + head.next = null + + return newHead +} From 10426cfb3d603330b216022bc0a2eca842291a28 Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 25 Jan 2025 22:18:53 +0900 Subject: [PATCH 2/2] feat: number-of-islands, longest-substring-without-repeating-characters solution --- .../YeomChaeeun.ts | 28 +++++++++++++ number-of-islands/YeomChaeeun.ts | 39 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 longest-substring-without-repeating-characters/YeomChaeeun.ts create mode 100644 number-of-islands/YeomChaeeun.ts diff --git a/longest-substring-without-repeating-characters/YeomChaeeun.ts b/longest-substring-without-repeating-characters/YeomChaeeun.ts new file mode 100644 index 000000000..ee3db5108 --- /dev/null +++ b/longest-substring-without-repeating-characters/YeomChaeeun.ts @@ -0,0 +1,28 @@ +/** + * 중복된 문자가 없는 가장 긴 부분 문자열 구하기 + * 달레 알고리즘 해설을 참고하여 작성했습니다 + * 슬라이딩 윈도우 방식 적용 + * 알고리즘 복잡도 + * - 시간 복잡도: O(n) + * - 공간 복잡도: O(n) + * @param s + */ +function lengthOfLongestSubstring(s: string): number { + const set = new Set(); + let start = 0; + let end = 0; + let maxLength = 0; + + while (end < s.length) { + if (set.has(s[end])) { + set.delete(s[start]) + start++ + } else { + set.add(s[end]) + maxLength = Math.max(maxLength, set.size) + end++ + } + } + return maxLength +} + diff --git a/number-of-islands/YeomChaeeun.ts b/number-of-islands/YeomChaeeun.ts new file mode 100644 index 000000000..b19695b78 --- /dev/null +++ b/number-of-islands/YeomChaeeun.ts @@ -0,0 +1,39 @@ +/** + * 2차 배열의 1로된 섬 구하기 + * 달레 알고리즘 해석을 참고하여 작성했습니다. + * DFS(깊이 우선 알고리즘) + * 알고리즘 복잡도 + * - 시간 복잡도: O(m x n) + * - 공간 복잡도: O(m x n) + * @param grid + */ +function numIslands(grid: string[][]): number { + const rows = grid.length + const cols = grid[0].length + let islands = 0 + + // 인접한 땅의 1을 찾아야 함 - 상하좌우 + const dfs = (i: number, j: number): void => { + if (i < 0 || i >= rows || j < 0 || j >= cols || grid[i][j] !== '1') { + return + } + + grid[i][j] = '0' // 한번 확인한 경우 물로 바꿔줌 + + dfs(i + 1, j) + dfs(i - 1, j) + dfs(i, j + 1) + dfs(i, j - 1) + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === '1') { + dfs(i, j) + islands++ + } + } + } + + return islands +}