From aa40bb1eeb34b7419b6344571654f2ad2e457150 Mon Sep 17 00:00:00 2001 From: hsskey Date: Thu, 8 May 2025 08:35:07 +0900 Subject: [PATCH 1/5] valid-parentheses solved --- valid-parentheses/hsskey.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-parentheses/hsskey.js 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 +}; From 61e78ebbb8b619eed75942f6a3df7c08cdc3d91d Mon Sep 17 00:00:00 2001 From: hsskey Date: Fri, 9 May 2025 22:13:17 +0900 Subject: [PATCH 2/5] container-with-most-water solved --- container-with-most-water/hsskey.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 container-with-most-water/hsskey.js 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 +}; From 12924f4306c80db478ed54a043904b121f6220b3 Mon Sep 17 00:00:00 2001 From: hsskey Date: Fri, 9 May 2025 22:24:32 +0900 Subject: [PATCH 3/5] design-add-and-search-words-data-structure solved --- .../hsskey.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 design-add-and-search-words-data-structure/hsskey.js 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); + } +} From 938990f1b75baa8e9ac07fcdea13124e7077735f Mon Sep 17 00:00:00 2001 From: hsskey Date: Fri, 9 May 2025 22:30:06 +0900 Subject: [PATCH 4/5] longest-increasing-subsequence solved --- longest-increasing-subsequence/hsskey.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 longest-increasing-subsequence/hsskey.js 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) +}; From 116b16f0fafc314ed78182bc65a0e69fbaa3357a Mon Sep 17 00:00:00 2001 From: hsskey Date: Fri, 9 May 2025 22:36:35 +0900 Subject: [PATCH 5/5] spiral-matrix solved --- spiral-matrix/hsskey.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spiral-matrix/hsskey.js 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; +};