diff --git a/container-with-most-water/hsskey.js b/container-with-most-water/hsskey.js new file mode 100644 index 000000000..6cf3f9aa1 --- /dev/null +++ b/container-with-most-water/hsskey.js @@ -0,0 +1,21 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let result = 0 + let l = 0; + let r = height.length - 1; + + while(l < r) { + const area = (r - l) * Math.min(height[l], height[r]); + result = Math.max(result, area); + + if(height[l] < height[r]) { + l += 1; + } else { + r -= 1; + } + } + return result +}; diff --git a/design-add-and-search-words-data-structure/hsskey.js b/design-add-and-search-words-data-structure/hsskey.js new file mode 100644 index 000000000..e3988520e --- /dev/null +++ b/design-add-and-search-words-data-structure/hsskey.js @@ -0,0 +1,47 @@ +class TrieNode { + constructor() { + this.children = {}; + this.word = false; + } +} + +class WordDictionary { + constructor() { + this.root = new TrieNode(); + } + + addWord(word) { + let cur = this.root; + for (let c of word) { + if (!cur.children[c]) { + cur.children[c] = new TrieNode(); + } + cur = cur.children[c]; + } + cur.word = true; + } + + search(word) { + const dfs = (j, root) => { + let cur = root; + for (let i = j; i < word.length; i++) { + const c = word[i]; + if (c === '.') { + for (let child of Object.values(cur.children)) { + if (dfs(i + 1, child)) { + return true; + } + } + return false; + } else { + if (!cur.children[c]) { + return false; + } + cur = cur.children[c]; + } + } + return cur.word; + }; + return dfs(0, this.root); + } +} diff --git a/longest-increasing-subsequence/hsskey.js b/longest-increasing-subsequence/hsskey.js new file mode 100644 index 000000000..8954c110d --- /dev/null +++ b/longest-increasing-subsequence/hsskey.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var lengthOfLIS = function(nums) { + const lis = new Array(nums.length).fill(1); + + for(let i = nums.length - 2; i >= 0; i--) { + for(let j = i + 1; j < nums.length; j++) { + if(nums[i] < nums[j]) { + lis[i] = Math.max(lis[i], 1 + lis[j]) + } + } + } + return Math.max(...lis) +}; diff --git a/spiral-matrix/hsskey.js b/spiral-matrix/hsskey.js new file mode 100644 index 000000000..d00a57192 --- /dev/null +++ b/spiral-matrix/hsskey.js @@ -0,0 +1,41 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if (matrix.length === 0) return []; + + const res = []; + let left = 0, right = matrix[0].length; + let top = 0, bottom = matrix.length; + + while (left < right && top < bottom) { + // 상단 행 왼쪽 → 오른쪽 + for (let i = left; i < right; i++) { + res.push(matrix[top][i]); + } + top += 1; + + // 오른쪽 열 위 → 아래 + for (let i = top; i < bottom; i++) { + res.push(matrix[i][right - 1]); + } + right -= 1; + + if (!(left < right && top < bottom)) break; + + // 하단 행 오른쪽 → 왼쪽 + for (let i = right - 1; i >= left; i--) { + res.push(matrix[bottom - 1][i]); + } + bottom -= 1; + + // 왼쪽 열 아래 → 위 + for (let i = bottom - 1; i >= top; i--) { + res.push(matrix[i][left]); + } + left += 1; + } + + return res; +}; diff --git a/valid-parentheses/hsskey.js b/valid-parentheses/hsskey.js new file mode 100644 index 000000000..8f6415ea4 --- /dev/null +++ b/valid-parentheses/hsskey.js @@ -0,0 +1,25 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + const stack = [] + + const pair = { + ')' : '(', + '}' : '{', + ']' : '[' + } + + for (let item of s) { + // 여는 괄호 + if(item === '(' || item === '{' || item === '[') { + stack.push(item) + // stack길이가 0 or 닫는 괄호 케이스 + } else if (stack.length === 0 || stack.pop() !== pair[item]) { + return false + } + } + + return stack.length === 0 +};