From 2576e200c6d9449963e8ffd8eec923057a175676 Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Fri, 9 May 2025 10:28:28 +0900 Subject: [PATCH 1/3] valid-parentheses solution --- valid-parentheses/moonjonghoo.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 valid-parentheses/moonjonghoo.js diff --git a/valid-parentheses/moonjonghoo.js b/valid-parentheses/moonjonghoo.js new file mode 100644 index 000000000..6e8868bae --- /dev/null +++ b/valid-parentheses/moonjonghoo.js @@ -0,0 +1,18 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function (s) { + let object = { ")": "(", "}": "{", "]": "[" }; + let stack = []; + for (let char of s) { + if (char === "(" || char === "{" || char === "[") { + stack.push(char); + } else { + if (stack.length === 0 || stack.pop() !== object[char]) { + return false; + } + } + } + return stack.length === 0; +}; From 25a75aab23bb91fcce5bc1d64492d27324a413f2 Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Fri, 9 May 2025 13:12:14 +0900 Subject: [PATCH 2/3] container with most water solution --- container-with-most-water/moonjonghoo.js | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 container-with-most-water/moonjonghoo.js diff --git a/container-with-most-water/moonjonghoo.js b/container-with-most-water/moonjonghoo.js new file mode 100644 index 000000000..d9705fff9 --- /dev/null +++ b/container-with-most-water/moonjonghoo.js @@ -0,0 +1,26 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function (height) { + let max_area = 0; + let left = 0; + let right = height.length - 1; + while (left < right) { + let current_x = right - left; + let current_y = Math.min(height[left], height[right]); + let current_area = current_x * current_y; + + if (current_area > max_area) { + max_area = current_area; + } + if (height[left] < height[right]) { + left = left + 1; + } else { + right = right - 1; + } + } + return max_area; +}; + +console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])); From 3e4ebe14756c7e9333d0dccc825a276babfb676a Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Fri, 9 May 2025 14:40:38 +0900 Subject: [PATCH 3/3] desing add and search words data structure solution --- .../moonjonghoo.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 design-add-and-search-words-data-structure/moonjonghoo.js diff --git a/design-add-and-search-words-data-structure/moonjonghoo.js b/design-add-and-search-words-data-structure/moonjonghoo.js new file mode 100644 index 000000000..316018875 --- /dev/null +++ b/design-add-and-search-words-data-structure/moonjonghoo.js @@ -0,0 +1,36 @@ +const TrieNode = function () { + this.children = {}; + this.isEndOfWord = false; +}; +var WordDictionary = function () { + this.root = new TrieNode(); +}; + +/** + * @param {string} word + * @return {void} + */ +WordDictionary.prototype.addWord = function (word) { + let currentNode = this.root; + for (let i = 0; i < word.length; i++) { + let char = word[i]; + if (!currentNode.children[char]) { + currentNode.children[char] = new TrieNode(); + } + currentNode = currentNode.children[char]; + } + currentNode.isEndOfWord = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +WordDictionary.prototype.search = function (word) {}; + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */