diff --git a/container-with-most-water/hoyeongkwak.ts b/container-with-most-water/hoyeongkwak.ts new file mode 100644 index 000000000..d45b05019 --- /dev/null +++ b/container-with-most-water/hoyeongkwak.ts @@ -0,0 +1,15 @@ +function maxArea(height: number[]): number { + let res = 0 + let l = 0 + let r = height.length - 1 + while (l < r) { + const area = (r - l) * Math.min(height[l], height[r]) + res = Math.max(res, area) + if (height[l] > height[r]) { + r -= 1 + } else { + l += 1 + } + } + return res +}; diff --git a/design-add-and-search-words-data-structure/hoyeongkwak.ts b/design-add-and-search-words-data-structure/hoyeongkwak.ts new file mode 100644 index 000000000..6578f7c80 --- /dev/null +++ b/design-add-and-search-words-data-structure/hoyeongkwak.ts @@ -0,0 +1,63 @@ +/* +O(n) +O(n) +*/ + +class TriedNode { + children: Map + isEndOfWord: boolean + + constructor(){ + this.children = new Map() + this.isEndOfWord = false + } +} + +class WordDictionary { + root: TriedNode + constructor() { + this.root = new TriedNode() + } + + addWord(word: string): void { + let node = this.root + for (const char of word) { + if (!node.children.has(char)) { + node.children.set(char, new TriedNode()) + } + node = node.children.get(char) + } + node.isEndOfWord = true + } + + search(word: string): boolean { + const searchInNode = (word: string, index: number, node: TriedNode): boolean => { + if (index === word.length) return node.isEndOfWord + + const char = word[index] + + if (char === '.') { + for (const [, childNode] of node.children) { + if (searchInNode(word, index + 1, childNode)) { + return true + } + } + return false + } else { + if (!node.children.has(char)) { + return false + } + return searchInNode(word, index + 1, node.children.get(char)) + } + } + return searchInNode(word, 0, this.root) + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */ + diff --git a/longest-increasing-subsequence/hoyeongkwak.ts b/longest-increasing-subsequence/hoyeongkwak.ts new file mode 100644 index 000000000..c01b65929 --- /dev/null +++ b/longest-increasing-subsequence/hoyeongkwak.ts @@ -0,0 +1,17 @@ +function lengthOfLIS(nums: number[]): number { + let res = 1 + const dp: number[] = Array.from(nums).fill(1) + for (let i = nums.length - 2; i >= 0; i--) { + let curr = 1 + let j = i + while(j < nums.length && curr < res + 1) { + if (nums[j] > nums[i]) { + curr = Math.max(curr, 1 + dp[j]) + } + j++ + } + dp[i] = curr + res = Math.max(dp[i], res) + } + return res +}; diff --git a/longest-substring-without-repeating-characters/hoyeongkwak.ts b/longest-substring-without-repeating-characters/hoyeongkwak.ts new file mode 100644 index 000000000..9ee9ab924 --- /dev/null +++ b/longest-substring-without-repeating-characters/hoyeongkwak.ts @@ -0,0 +1,33 @@ +/* + abc a l = 3 + bca b l = 3 + cab c l = 3 + abc b l = 1 + cb + + pw w l = 2 + wke w l = 3 + w +*/ +/* + time complexity : O(n) + space complexity : O(n) +*/ +function lengthOfLongestSubstring(s: string): number { + if (s.length === 0) return 0 + + let maxLen = 0 + let start = 0 + const charMap = new Map() + + for (let end = 0; end < s.length; end++) { + const currChar = s[end] + + if (charMap.has(currChar) && charMap.get(currChar)! >= start) { + start = charMap.get(currChar)! + 1 + } + charMap.set(currChar, end) + maxLen = Math.max(maxLen, end - start + 1) + } + return maxLen +}; diff --git a/number-of-islands/hoyeongkwak.ts b/number-of-islands/hoyeongkwak.ts new file mode 100644 index 000000000..623e6358c --- /dev/null +++ b/number-of-islands/hoyeongkwak.ts @@ -0,0 +1,32 @@ +/* + time complexity : O(m * n) + space complexity : O(m) +*/ +function numIslands(grid: string[][]): number { + const rows = grid.length + const cols = grid[0].length + const direction = [[1, 0], [-1, 0], [0, 1], [0, -1]] + let islands = 0 + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (grid[r][c] === '1') { + islands += 1 + const queue = new Array() + + grid[r][c] = '0' + queue.push([r, c]) + while (queue.length > 0) { + let curr = queue.shift() + for (const dir of direction) { + let dr = curr[0] + dir[0] + let dc = curr[1] + dir[1] + if (dr < 0 || dr >= rows || dc < 0 || dc >= cols || grid[dr][dc] === '0') continue + grid[dr][dc] = '0' + queue.push([dr, dc]) + } + } + } + } + } + return islands +}; diff --git a/reverse-linked-list/hoyeongkwak.ts b/reverse-linked-list/hoyeongkwak.ts new file mode 100644 index 000000000..cf7aa2be2 --- /dev/null +++ b/reverse-linked-list/hoyeongkwak.ts @@ -0,0 +1,36 @@ +/** + * 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) + * } + * } + */ + +/* +curr prev +1, 2, 3, 4, 5 null +2, 3, 4, 5 1 +3, 4, 5 2, 1 +4, 5 3, 2, 1 +5 4, 3, 2, 1 +null 5, 4, 3, 2, 1 +*/ +/* + time complexity : O(n) + space complexity : O(1) +*/ +function reverseList(head: ListNode | null): ListNode | null { + let curr = head + let prev = null + while (curr) { + let next = curr.next + curr.next = prev + prev = curr + curr = next + } + return prev +}; diff --git a/set-matrix-zeroes/hoyeongkwak.ts b/set-matrix-zeroes/hoyeongkwak.ts new file mode 100644 index 000000000..33cfda3a4 --- /dev/null +++ b/set-matrix-zeroes/hoyeongkwak.ts @@ -0,0 +1,28 @@ +/* + (0, 0) 0~m, 0~n + (1, 1) (0~m, 1) (1, 0~n) + + time complexity : O(m * n) + space complexity : O(m * n) +*/ +function setZeroes(matrix: number[][]): void { + const rows = matrix.length + const cols = matrix[0].length + const zeroPosition = [] + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (matrix[r][c] === 0) { + zeroPosition.push([r,c]) + } + } + } + + for (let m = 0; m < zeroPosition.length; m++) { + const r = zeroPosition[m][0] + const c = zeroPosition[m][1] + matrix[r].fill(0) + for (let i = 0; i < rows; i++) { + matrix[i][c] = 0 + } + } +}; diff --git a/spiral-matrix/hoyeongkwak.ts b/spiral-matrix/hoyeongkwak.ts new file mode 100644 index 000000000..a963bd8c9 --- /dev/null +++ b/spiral-matrix/hoyeongkwak.ts @@ -0,0 +1,34 @@ +function spiralOrder(matrix: number[][]): number[] { + const m = matrix.length + const n = matrix[0].length + const size = m * n + let traversed = 0 + let top = 0 + let bottom = m - 1 + let left = 0 + let right = n - 1 + const output = [] + + while (traversed < size) { + for (let i = left; traversed < size && i <= right; i++, traversed++) { + output.push(matrix[top][i]) + } + top++ + + for (let i = top; traversed < size && i <= bottom; i++, traversed++) { + output.push(matrix[i][right]) + } + right-- + + for (let i = right; traversed < size && i >= left; i--, traversed++) { + output.push(matrix[bottom][i]) + } + bottom-- + + for (let i = bottom; traversed < size && i >= top; i--, traversed++) { + output.push(matrix[i][left]) + } + left++ + } + return output +}; diff --git a/unique-paths/hoyeongkwak.ts b/unique-paths/hoyeongkwak.ts new file mode 100644 index 000000000..8f527773c --- /dev/null +++ b/unique-paths/hoyeongkwak.ts @@ -0,0 +1,14 @@ +/* + time complexity : O(m * n) + space complexity : O(m * n) +*/ +function uniquePaths(m: number, n: number): number { + const dp = Array.from({ length: m }, () => Array(n).fill(1)) + let results = 0 + for (let r = 1; r < m; r++) { + for (let c = 1; c < n; c++) { + dp[r][c] = dp[r - 1][c] + dp[r][c - 1] + } + } + return dp[m-1][n-1] +}; diff --git a/valid-parentheses/hoyeongkwak.ts b/valid-parentheses/hoyeongkwak.ts new file mode 100644 index 000000000..f255ccc0c --- /dev/null +++ b/valid-parentheses/hoyeongkwak.ts @@ -0,0 +1,18 @@ +function isValid(s: string): boolean { + const stack: string[] = [] + const strMap = new Map([ + ['(', ')'], + ['[', ']'] , + ['{', '}'] + ]) + for (let str of s) { + if (strMap.has(str)) { + stack.push(strMap.get(str)) + } else if (stack.length > 0 && stack[stack.length - 1] === str) { + stack.pop() + } else { + return false + } + } + return stack.length === 0 +};