From 9c736ad495a15725bab99c5bd0856e77b71ca1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=B8=EC=B0=AC?= Date: Tue, 29 Apr 2025 17:33:03 +0900 Subject: [PATCH 1/5] best-time-to-buy-and-sell-stock solution --- best-time-to-buy-and-sell-stock/lhc0506.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/lhc0506.js diff --git a/best-time-to-buy-and-sell-stock/lhc0506.js b/best-time-to-buy-and-sell-stock/lhc0506.js new file mode 100644 index 000000000..38d857575 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/lhc0506.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let minPrice = Number.MAX_SAFE_INTEGER; + let maxProfit = 0; + + for (let i = 0; i < prices.length; i++) { + minPrice = Math.min(minPrice, prices[i]); + maxProfit = Math.max(maxProfit, prices[i] - minPrice); + } + + return maxProfit; +}; From a3328d7b7adf15a6843858cd2b8acc1f7c181936 Mon Sep 17 00:00:00 2001 From: Chan Date: Wed, 30 Apr 2025 22:52:27 +0900 Subject: [PATCH 2/5] group-anagrams solution --- group-anagrams/lhc0506.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 group-anagrams/lhc0506.js diff --git a/group-anagrams/lhc0506.js b/group-anagrams/lhc0506.js new file mode 100644 index 000000000..0f07269de --- /dev/null +++ b/group-anagrams/lhc0506.js @@ -0,0 +1,33 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + const A_ASCII = 'a'.charCodeAt(); + + const anagramMap = new Map(); + + for (const str of strs) { + const counter = new Array(26).fill(0); + + for (let i = 0; i < str.length; i++) { + counter[str.charCodeAt(i) - A_ASCII]++; + } + + let key = ''; + for (let i = 0; i < 26; i++) { + if (counter[i] > 0) { + key += String.fromCharCode(i + A_ASCII) + counter[i]; + } + } + + if (!anagramMap.has(key)) { + anagramMap.set(key, []); + } + anagramMap.get(key).push(str); + } + + return Array.from(anagramMap.values()); +}; + +// 시간 복잡도: O(n), 공간 복잡도: O(n) From 62ef031e610b46aeb00673acd89be05be89c78b9 Mon Sep 17 00:00:00 2001 From: Chan Date: Fri, 2 May 2025 18:52:28 +0900 Subject: [PATCH 3/5] implement-trie-prefix-tree solution --- implement-trie-prefix-tree/lhc0506.js | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 implement-trie-prefix-tree/lhc0506.js diff --git a/implement-trie-prefix-tree/lhc0506.js b/implement-trie-prefix-tree/lhc0506.js new file mode 100644 index 000000000..51ef8657a --- /dev/null +++ b/implement-trie-prefix-tree/lhc0506.js @@ -0,0 +1,65 @@ + +var Trie = function() { + this.isEnd = false; +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function(word) { + let currentTrie = this; + word.split('').forEach(alphabet => { + if (!currentTrie[alphabet]) { + currentTrie[alphabet] = new Trie(); + } + currentTrie = currentTrie[alphabet]; + }); + + currentTrie.isEnd = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function(word) { + let currentTrie = this; + + for (let alphabet of word) { + if (!currentTrie[alphabet]) { + return false; + } + currentTrie = currentTrie[alphabet]; + } + + return currentTrie.isEnd; +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function(prefix) { + let currentTrie = this; + + for (let alphabet of prefix) { + if (!currentTrie[alphabet]) { + return false; + } + currentTrie = currentTrie[alphabet]; + } + + return true; +}; + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ + + +// insert 시간복잡도: O(n), search 시간복잡도: O(n), startsWith 시간복잡도: O(n) From c3847f1cd0c391c0fc37906c38f493094ca4ff76 Mon Sep 17 00:00:00 2001 From: Chan Date: Sat, 3 May 2025 16:40:36 +0900 Subject: [PATCH 4/5] word-break solution --- word-break/lhc0506.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 word-break/lhc0506.js diff --git a/word-break/lhc0506.js b/word-break/lhc0506.js new file mode 100644 index 000000000..249225ed4 --- /dev/null +++ b/word-break/lhc0506.js @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function(s, wordDict) { + const wordDictSet = new Set(wordDict); + const visited = new Set(); + + const indexStack = [0]; + while(indexStack.length > 0) { + const startIndex = indexStack.pop(); + + if (visited.has(startIndex)) continue; + visited.add(startIndex); + + if (startIndex === s.length) return true; + + for (let endIndex = startIndex + 1; endIndex <= s.length; endIndex++) { + const word = s.substring(startIndex, endIndex); + + if (wordDictSet.has(word)) { + indexStack.push(endIndex); + } + } + } + + return false; +}; From 5b98f1017748f2b204947b9f2788015f45885438 Mon Sep 17 00:00:00 2001 From: Chan Date: Sat, 3 May 2025 18:24:00 +0900 Subject: [PATCH 5/5] encode-and-decode-strings solution --- encode-and-decode-strings/lhc0506.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 encode-and-decode-strings/lhc0506.js diff --git a/encode-and-decode-strings/lhc0506.js b/encode-and-decode-strings/lhc0506.js new file mode 100644 index 000000000..5283397c0 --- /dev/null +++ b/encode-and-decode-strings/lhc0506.js @@ -0,0 +1,17 @@ +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + return JSON.stringify(strs); + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(str) { + return JSON.parse(str); + } +}