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(); +}; +