From 4844d5df346555915647e5e0590dbdd6028373ed Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 2 May 2025 17:23:15 +0900 Subject: [PATCH 1/5] group-anagrams solution --- group-anagrams/krokerdile.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 group-anagrams/krokerdile.js diff --git a/group-anagrams/krokerdile.js b/group-anagrams/krokerdile.js new file mode 100644 index 000000000..32f3ff0a5 --- /dev/null +++ b/group-anagrams/krokerdile.js @@ -0,0 +1,23 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + const dict = new Map(); + + strs.forEach(str => { + const sorted = str.split('').sort().join(''); + if (!dict.has(sorted)) { + dict.set(sorted, [str]); + } else { + dict.get(sorted).push(str); + } + }); + + // value 길이 기준 내림차순 정렬 + const result = [...dict.entries()] + .sort((a, b) => b[1].length - a[1].length) + .map(([_, group]) => group); // value (즉, 아나그램 그룹)만 꺼냄 + + return result; +}; From d676e73d900cdfd907dcf32afec342146d1f7113 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 2 May 2025 17:24:01 +0900 Subject: [PATCH 2/5] implement-trie-prefix-tree solution --- implement-trie-prefix-tree/krokerdile.js | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 implement-trie-prefix-tree/krokerdile.js diff --git a/implement-trie-prefix-tree/krokerdile.js b/implement-trie-prefix-tree/krokerdile.js new file mode 100644 index 000000000..06c870839 --- /dev/null +++ b/implement-trie-prefix-tree/krokerdile.js @@ -0,0 +1,42 @@ +var Trie = function() { + this.root = {}; // 루트는 빈 객체로 시작 +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function(word) { + let node = this.root; + for (let ch of word) { + if (!node[ch]) node[ch] = {}; + node = node[ch]; + } + node.isEnd = true; // 단어의 끝을 표시 +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function(word) { + let node = this.root; + for (let ch of word) { + if (!node[ch]) return false; + node = node[ch]; + } + return node.isEnd === true; // 단어의 끝이어야만 true +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function(prefix) { + let node = this.root; + for (let ch of prefix) { + if (!node[ch]) return false; + node = node[ch]; + } + return true; // 접두사만 매칭되면 true +}; From abcb7d324ac7565ac1dbab29672992e0275934b7 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 2 May 2025 17:24:52 +0900 Subject: [PATCH 3/5] word-break solution --- word-break/krokerdile.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 word-break/krokerdile.js diff --git a/word-break/krokerdile.js b/word-break/krokerdile.js new file mode 100644 index 000000000..85d91a186 --- /dev/null +++ b/word-break/krokerdile.js @@ -0,0 +1,22 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function(s, wordDict) { + const wordSet = new Set(wordDict); // 빠른 검색을 위한 Set + const dp = Array(s.length + 1).fill(false); + dp[0] = true; // 빈 문자열은 항상 가능 + + for (let i = 1; i <= s.length; i++) { + for (let j = 0; j < i; j++) { + const word = s.slice(j, i); + if (dp[j] && wordSet.has(word)) { + dp[i] = true; + break; // 더 이상 볼 필요 없음 + } + } + } + + return dp[s.length]; +}; From dedc040d548a3c06df6a70c247368aff9a2ba4aa Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 2 May 2025 17:26:23 +0900 Subject: [PATCH 4/5] best-time-to-buy-and-sell-stock solution --- best-time-to-buy-and-sell-stock/krokerdile.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/krokerdile.js diff --git a/best-time-to-buy-and-sell-stock/krokerdile.js b/best-time-to-buy-and-sell-stock/krokerdile.js new file mode 100644 index 000000000..bcf022aa5 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/krokerdile.js @@ -0,0 +1,14 @@ +var maxProfit = function(prices) { + let minPrice = Infinity; + let maxProfit = 0; + + for (let price of prices) { + if (price < minPrice) { + minPrice = price; // 더 싼 가격이 나타나면 갱신 + } else { + maxProfit = Math.max(maxProfit, price - minPrice); // 이익 갱신 + } + } + + return maxProfit; +}; From a92eb7ebe1c92efa44ac9ca74ca5f74fbe3a68c3 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 2 May 2025 17:28:19 +0900 Subject: [PATCH 5/5] encode-and-decode-string solution --- encode-and-decode-strings/krokerdile.js | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 encode-and-decode-strings/krokerdile.js diff --git a/encode-and-decode-strings/krokerdile.js b/encode-and-decode-strings/krokerdile.js new file mode 100644 index 000000000..529746753 --- /dev/null +++ b/encode-and-decode-strings/krokerdile.js @@ -0,0 +1,35 @@ +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + let result = ""; + + for (const str of strs) { + result += `${str.length}#${str}`; + } + + return result; + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(s) { + let result = []; + let i = 0; + // 5#hello5#world + while (i < s.length) { + const pos = s.indexOf("#", i); + const len = parseInt(s.slice(i, pos));// 5 + i = pos + 1; + const str = s.slice(i, i + len); + result.push(str); + i += len; + } + return result; + } +} +