From 24ae108b6828fb85eeb06644060a256005c8440e Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Thu, 1 May 2025 09:24:54 +0900 Subject: [PATCH 1/3] best-time-to-buy-and-sell-stock solution --- .../moonjonghoo.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/moonjonghoo.js diff --git a/best-time-to-buy-and-sell-stock/moonjonghoo.js b/best-time-to-buy-and-sell-stock/moonjonghoo.js new file mode 100644 index 000000000..9ac0378df --- /dev/null +++ b/best-time-to-buy-and-sell-stock/moonjonghoo.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let minPrices = Infinity; + let profit = 0; + for (price of prices) { + minPrices = Math.min(price, minPrices); + profit = Math.max(profit, price - minPrices); + } + return profit; +}; + +var maxProfit = function (prices) { + let maxProfit = 0; + let currentProfit = 0; + for (let i = 1; i < prices.length; i++) { + let diff = prices[i] - prices[i - 1]; + currentProfit = Math.max(0, currentProfit + diff); + maxProfit = Math.max(maxProfit, currentProfit); + } + return maxProfit; +}; From fc16417a7715899577a34223fef6f2f9b864e9c2 Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Thu, 1 May 2025 10:29:59 +0900 Subject: [PATCH 2/3] group-anagrams solution --- group-anagrams/moonjonghoo.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 group-anagrams/moonjonghoo.js diff --git a/group-anagrams/moonjonghoo.js b/group-anagrams/moonjonghoo.js new file mode 100644 index 000000000..fbc0f99a8 --- /dev/null +++ b/group-anagrams/moonjonghoo.js @@ -0,0 +1,22 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + let hashMap = new Map(); + for (let i = 0; i < strs.length; i++) { + let key = strs[i].split("").sort().join(""); + if (!hashMap.has(key)) { + hashMap.set(key, [strs[i]]); + } else { + hashMap.set(key, [...hashMap.get(key), strs[i]]); + } + } + let answer = []; + for ([key, value] of hashMap) { + answer.push(value); + } + return answer.sort((a, b) => a.length - b.length).map((arr) => arr.sort()); +}; + +console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); From fd8d099f19b7539bb7a7c5f67de607f4f5e4c8c4 Mon Sep 17 00:00:00 2001 From: moonjonghoo Date: Thu, 1 May 2025 12:10:04 +0900 Subject: [PATCH 3/3] wordBreak solution --- word-break/moonjonghoo.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 word-break/moonjonghoo.js diff --git a/word-break/moonjonghoo.js b/word-break/moonjonghoo.js new file mode 100644 index 000000000..6d4372b93 --- /dev/null +++ b/word-break/moonjonghoo.js @@ -0,0 +1,31 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function (s, wordDict) { + const memo = new Map(); // 실패/성공 여부 기억 + const wordSet = new Set(wordDict); // lookup 최적화 + + function canBreak(sub) { + if (sub === "") return true; + + if (memo.has(sub)) return memo.get(sub); + + for (let i = 1; i <= sub.length; i++) { + const prefix = sub.slice(0, i); + if (wordSet.has(prefix)) { + const suffix = sub.slice(i); + if (canBreak(suffix)) { + memo.set(sub, true); + return true; + } + } + } + + memo.set(sub, false); // 실패한 경우도 기억 + return false; + } + + return canBreak(s); +};