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; +}; 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; + } +} + 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; +}; 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 +}; 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]; +};