From 3ee27d79f732ea05f7dd3ec94ddc1a5a24c31757 Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Tue, 7 Jan 2025 16:12:11 -0800 Subject: [PATCH 1/3] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/yoonthecoder.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/yoonthecoder.js diff --git a/best-time-to-buy-and-sell-stock/yoonthecoder.js b/best-time-to-buy-and-sell-stock/yoonthecoder.js new file mode 100644 index 000000000..839f9a941 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/yoonthecoder.js @@ -0,0 +1,13 @@ +var maxProfit = function (prices) { + // set the initial value to Infinity so that it can always return minimum value + let minPrice = Infinity; + let maxPrice = 0; + for (i = 0; i < prices.length; i++) { + minPrice = Math.min(prices[i], minPrice); + maxPrice = Math.max(maxPrice, prices[i + 1] - minPrice); + } + return maxPrice; +}; + +// Time complexity: O(n) - Iterating through the array +// Space complexity: O(1) From 977921d78612aa9f6466dbbbbccb79bc1c93d99b Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Thu, 9 Jan 2025 02:46:25 -0800 Subject: [PATCH 2/3] group anagrams solution --- group-anagrams/yoonthecoder.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 group-anagrams/yoonthecoder.js diff --git a/group-anagrams/yoonthecoder.js b/group-anagrams/yoonthecoder.js new file mode 100644 index 000000000..42ea7ac35 --- /dev/null +++ b/group-anagrams/yoonthecoder.js @@ -0,0 +1,14 @@ +var groupAnagrams = function (strs) { + const map = new Map(); + + for (const str of strs) { + // split the string into each character (returns an array) => sort it => convert it to string + const sortedStr = str.split('').sort().join(''); + // use the sorted Str as unique keys in the map + if (map.has(sortedStr)) { + map.get(sortedStr).push(str); + } else map.set(sortedStr, [str]); + } + // convert values into an array + return [...map.values()]; +}; From fd476db9a36befe7c44a96fc26d88eec5642e6ab Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Sat, 11 Jan 2025 02:36:36 -0800 Subject: [PATCH 3/3] 3sum solution --- 3sum/yoonthecoder.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 3sum/yoonthecoder.js diff --git a/3sum/yoonthecoder.js b/3sum/yoonthecoder.js new file mode 100644 index 000000000..2af3dc01e --- /dev/null +++ b/3sum/yoonthecoder.js @@ -0,0 +1,27 @@ +var threeSum = function (nums) { + nums.sort((a, b) => a - b); + const results = []; + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + let left = i + 1; + let right = nums.length - 1; + while (left < right) { + const currSum = nums[i] + nums[left] + nums[right]; + + if (currSum == 0) { + results.push([nums[i], nums[left], nums[right]]); + left++; + right--; + // to avoid duplicates + while (left < right && nums[left] === nums[left - 1]) left++; + while (left < right && nums[right] === nums[right + 1]) right--; + } else if (currSum < 0) { + left++; + } else right--; + } + } + return results; +}; + +// Time complexity: O(n^2); +// Space complexity: O(n)