diff --git a/best-time-to-buy-and-sell-stock/hyejjun.js b/best-time-to-buy-and-sell-stock/hyejjun.js new file mode 100644 index 000000000..048b453d0 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hyejjun.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let minPrice = Infinity; + let maxProfit = 0; + + for (let i = 0; i < prices.length; i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + maxProfit = Math.max(maxProfit, prices[i] - minPrice); + } + } + + return maxProfit; + +}; + +console.log(maxProfit([7, 1, 5, 3, 6, 4])); +console.log(maxProfit([7, 6, 4, 3, 1])); + +/* +시간 복잡도: O(n) +공간 복잡도: O(1) +*/ diff --git a/group-anagrams/hyejjun.js b/group-anagrams/hyejjun.js new file mode 100644 index 000000000..8d9f11a97 --- /dev/null +++ b/group-anagrams/hyejjun.js @@ -0,0 +1,32 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + let map = new Map(); + + for (let str of strs) { + let sortedStr = str.split('').sort().join(''); + + if (map.has(sortedStr)) { + map.get(sortedStr).push(str); + } else { + map.set(sortedStr, [str]); + } + } + + return Array.from(map.values()); +}; + +console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); +console.log(groupAnagrams([""])); +console.log(groupAnagrams(["a"])); + + +/* +시간 복잡도: O(n*k log k) +공간 복잡도: O(n*k) + +n: 문자열의 개수 +k: 문자열의 최대 길이 +*/