diff --git a/longest-substring-without-repeating-characters/hyer0705.ts b/longest-substring-without-repeating-characters/hyer0705.ts new file mode 100644 index 000000000..c2dd6d68a --- /dev/null +++ b/longest-substring-without-repeating-characters/hyer0705.ts @@ -0,0 +1,19 @@ +function lengthOfLongestSubstring(s: string): number { + let maxLen = 0; + + const used = new Set(); + let windowStart = 0; + + for (let windowEnd = 0; windowEnd < s.length; windowEnd++) { + const currentCh = s[windowEnd]; + while (used.has(currentCh)) { + used.delete(s[windowStart]); + windowStart++; + } + + used.add(currentCh); + maxLen = Math.max(maxLen, windowEnd - windowStart + 1); + } + + return maxLen; +} diff --git a/number-of-islands/hyer0705.ts b/number-of-islands/hyer0705.ts new file mode 100644 index 000000000..824fbc807 --- /dev/null +++ b/number-of-islands/hyer0705.ts @@ -0,0 +1,52 @@ +function numIslands(grid: string[][]): number { + const m = grid.length; + const n = grid[0].length; + + const LAND = "1"; + const WATER = "0"; + + const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); + + const isValid = (row: number, col: number): boolean => row >= 0 && row < m && col >= 0 && col < n; + + const bfs = (row: number, col: number): void => { + const directions = [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ]; + + // [row, col][] + const queue: number[][] = []; + + visited[row][col] = true; + queue.push([row, col]); + + while (queue.length > 0) { + const [cx, cy] = queue.shift()!; + + for (const [dx, dy] of directions) { + const [nx, ny] = [cx + dx, cy + dy]; + + if (isValid(nx, ny) && !visited[nx][ny] && grid[nx][ny] === LAND) { + visited[nx][ny] = true; + queue.push([nx, ny]); + } + } + } + }; + + let island = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === LAND && !visited[i][j]) { + island++; + bfs(i, j); + } + } + } + + return island; +} diff --git a/reverse-linked-list/hyer0705.ts b/reverse-linked-list/hyer0705.ts new file mode 100644 index 000000000..a009b2902 --- /dev/null +++ b/reverse-linked-list/hyer0705.ts @@ -0,0 +1,40 @@ +/** + * 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) + * } + * } + */ + +// using iterative +function reverseList(head: ListNode | null): ListNode | null { + if (!head) return null; + + let prev: ListNode | null = null; + let current = head; + + while (current) { + const temp: ListNode | null = current.next; + current.next = prev; + prev = current; + current = temp; + } + + return prev; +} + +// using recursive +function reverseList(head: ListNode | null): ListNode | null { + if (!head || !head.next) return head; + + const newHead = reverseList(head.next); + + head.next.next = head; + head.next = null; + + return newHead; +} diff --git a/set-matrix-zeroes/hyer0705.ts b/set-matrix-zeroes/hyer0705.ts new file mode 100644 index 000000000..80d127187 --- /dev/null +++ b/set-matrix-zeroes/hyer0705.ts @@ -0,0 +1,70 @@ +// mark first row, first col - 0ms +/** + Do not return anything, modify matrix in-place instead. + */ +function setZeroes(matrix: number[][]): void { + const m = matrix.length; + const n = matrix[0].length; + + let isFirstColZero = false; + let isFirstRowZero = false; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] === 0) { + if (!isFirstRowZero && i === 0) isFirstRowZero = true; + if (!isFirstColZero && j === 0) isFirstColZero = true; + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + if (matrix[i][0] === 0 || matrix[0][j] === 0) { + matrix[i][j] = 0; + } + } + } + + if (isFirstRowZero) { + for (let j = 0; j < n; j++) { + matrix[0][j] = 0; + } + } + if (isFirstColZero) { + for (let i = 0; i < m; i++) { + matrix[i][0] = 0; + } + } +} + +// using set - 4ms +/** + Do not return anything, modify matrix in-place instead. + */ +function setZeroes(matrix: number[][]): void { + const m = matrix.length; + const n = matrix[0].length; + + // `${row},${col}` + const coordinates = new Set(); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] === 0) { + coordinates.add(`${i},${j}`); + } + } + } + + for (const coordinate of coordinates) { + const [x, y] = coordinate.split(","); + for (let j = 0; j < n; j++) { + matrix[x][j] = 0; + } + for (let i = 0; i < m; i++) { + matrix[i][y] = 0; + } + } +} diff --git a/unique-paths/hyer0705.ts b/unique-paths/hyer0705.ts new file mode 100644 index 000000000..1d3618e9d --- /dev/null +++ b/unique-paths/hyer0705.ts @@ -0,0 +1,11 @@ +function uniquePaths(m: number, n: number): number { + const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1)); + + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + return dp[m - 1][n - 1]; +}