diff --git a/best-time-to-buy-and-sell-stock/uraflower.js b/best-time-to-buy-and-sell-stock/uraflower.js new file mode 100644 index 000000000..8012dfe8b --- /dev/null +++ b/best-time-to-buy-and-sell-stock/uraflower.js @@ -0,0 +1,18 @@ +/** + * 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수 + * @param {number[]} prices + * @return {number} + */ +const maxProfit = function(prices) { + let min = prices[0]; + let profit = 0; + + for (const price of prices) { + min = Math.min(min, price); + profit = Math.max(profit, price - min); + } + + return profit; +}; +// 시간복잡도: O(n) +// 공간복잡도: O(1) diff --git a/encode-and-decode-strings/uraflower.js b/encode-and-decode-strings/uraflower.js new file mode 100644 index 000000000..c1e16d8ef --- /dev/null +++ b/encode-and-decode-strings/uraflower.js @@ -0,0 +1,16 @@ +const encode = function (strs) { + const separator = '\\'; + return strs.join(separator); +} + + +const decode = function (str) { + const separator = '\\'; + return str.split(separator); +} + +// 문제가 너무 별로다 +// 문제에서 제시하고 있는 해답도 별로다 +// 이모지같은 걸 구분자로 쓰거나, length를 앞에 넣어서 구별하거나 해도 사실 제대로 암호화했다고 말할 수 없음 +// 그런 걸 정답으로 제공할 거면 이런 문제를 왜 내는 거여 +// 이 문제가 Blind 75에 속해 있는 이유가 뭘까...?! diff --git a/group-anagrams/uraflower.js b/group-anagrams/uraflower.js new file mode 100644 index 000000000..c014f7398 --- /dev/null +++ b/group-anagrams/uraflower.js @@ -0,0 +1,32 @@ +/** + * 애너그램끼리 묶어서 반환하는 함수 + * @param {string[]} strs + * @return {string[][]} + */ +const groupAnagrams = function(strs) { + // 풀이 1 + // 시간복잡도: O(n*s) (n: strs.length, s: str.length) + // 공간복잡도: O(n) + function groupManually () { + const groups = {}; + + strs.forEach((str) => { + const key = [...str].sort().join(''); + if (!groups[key]) groups[key] = []; + groups[key].push(str); + }); + + return Object.values(groups); + } + + // 풀이 2 + // 시간복잡도: O(n*s) (n: strs.length, s: str.length) + // 공간복잡도: O(n) + function groupByAnagram() { + const result = Object.groupBy(strs, (str) => [...str].sort().join('')); + return Object.values(result); + } + + return groupByAnagram(); +}; + diff --git a/implement-trie-prefix-tree/uraflower.js b/implement-trie-prefix-tree/uraflower.js new file mode 100644 index 000000000..e342e6d08 --- /dev/null +++ b/implement-trie-prefix-tree/uraflower.js @@ -0,0 +1,65 @@ +const Node = function (value) { + this.value = value; + this.children = {}; + this.data = null; +} + +const Trie = function () { + this.root = new Node(null); +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function (word) { + let parent = this.root; + + for (let i = 0; i < word.length; i++) { + if (!parent.children[word[i]]) { + parent.children[word[i]] = new Node(word[i]); + } + parent = parent.children[word[i]]; + } + + parent.data = word; +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function (word) { + let parent = this.root; + let i = 0; + + while (i < word.length && parent.children[word[i]]) { + parent = parent.children[word[i]]; + i += 1; + } + + return parent.data === word; +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function (prefix) { + let parent = this.root; + + for (let char of prefix) { + if (!parent.children[char]) return false; + parent = parent.children[char]; + } + + 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) + */ diff --git a/word-break/uraflower.js b/word-break/uraflower.js new file mode 100644 index 000000000..4cbeb36e9 --- /dev/null +++ b/word-break/uraflower.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +const wordBreak = function (s, wordDict) { + const dp = Array(s.length); + const dict = new Set(wordDict); + + function recurse(start) { + if (start === s.length) return true; + if (dp[start] !== undefined) return dp[start]; + + for (let end = start + 1; end <= s.length; end++) { + const substr = s.slice(start, end); + if (dict.has(substr) && recurse(end)) { + dp[start] = true; + return true; + } + } + + dp[start] = false; + return false; + } + + return recurse(0); +}; + +// 시간복잡도: O(n^2) (n: s.length. n번 재귀 & 최대 n번 슬라이싱) +// 공간복잡도: O(n + m) (m: wordDict.length)