diff --git a/longest-substring-without-repeating-characters/hyejj19.ts b/longest-substring-without-repeating-characters/hyejj19.ts new file mode 100644 index 0000000000..e9d88103ca --- /dev/null +++ b/longest-substring-without-repeating-characters/hyejj19.ts @@ -0,0 +1,24 @@ +function lengthOfLongestSubstring(s: string): number { + if (s.length <= 1) return s.length; + + let left = 0; + let right = 0; + let maxLen = -1; + const sMap = new Map(); + while (right !== s.length) { + const curS = s[right]; + // Map에 존재하지 않는다 -> 중복 X -> right 전진 + if (!sMap.get(curS)) { + sMap.set(s[right], 1); + right++; + } else { + // Map에 존재 한다 -> 중복 O -> left 전진 + while (left <= right && sMap.get(curS)) { + sMap.set(s[left], sMap.get(s[left]) - 1); + left++; + } + } + maxLen = Math.max(maxLen, right - left); + } + return maxLen; +} diff --git a/number-of-islands/hyejj19.ts b/number-of-islands/hyejj19.ts new file mode 100644 index 0000000000..3409423e3f --- /dev/null +++ b/number-of-islands/hyejj19.ts @@ -0,0 +1,42 @@ +// sc : O(n*m) +// tc : O(n*m) +function numIslands(grid: string[][]): number { + let count = 0; + const visited = Array.from({length: grid.length}, () => + Array.from({length: grid[0].length}, () => false), + ); + const directions = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ]; + + function explore(x, y) { + visited[x][y] = true; + for (let [dx, dy] of directions) { + const nx = dx + x; + const ny = dy + y; + if ( + nx < grid.length && + ny < grid[0].length && + nx >= 0 && + ny >= 0 && + !visited[nx][ny] && + grid[nx][ny] === '1' + ) { + explore(nx, ny); + } + } + } + + for (let x = 0; x < grid.length; x++) { + for (let y = 0; y < grid[x].length; y++) { + if (!visited[x][y] && grid[x][y] === '1') { + count++; + explore(x, y); + } + } + } + return count; +} diff --git a/reverse-linked-list/hyejj19.ts b/reverse-linked-list/hyejj19.ts new file mode 100644 index 0000000000..f95d78ffdb --- /dev/null +++ b/reverse-linked-list/hyejj19.ts @@ -0,0 +1,18 @@ +/* +time complexity : O(n) +space complexity : O(1) +*/ +function reverseList(head: ListNode | null): ListNode | null { + let prev = null; + let cur = head; + let next = null; + + while (cur !== null) { + next = cur.next; + cur.next = prev; + prev = cur; + cur = next; + } + + return prev; +}