From 6c7105ca45c2ffd0e8634dd28e903a4599975cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tasha=28=EC=82=B4=EB=AF=B8=29?= <45252527+Lustellz@users.noreply.github.com> Date: Sat, 23 Aug 2025 23:11:52 +0900 Subject: [PATCH 1/2] maxProfit --- best-time-to-buy-and-sell-stock/Lustellz.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/Lustellz.ts diff --git a/best-time-to-buy-and-sell-stock/Lustellz.ts b/best-time-to-buy-and-sell-stock/Lustellz.ts new file mode 100644 index 000000000..1e86a7a70 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Lustellz.ts @@ -0,0 +1,16 @@ +function maxProfit(prices: number[]): number { + + let minPrice: number = prices[0]; + let maxProfit: number = 0; + + for (let i = 1; i < prices.length; i++) { + // 1. compare the number later than now + if (prices[i] < minPrice) { + // 2. if there's bigger number later, set it as the standard + minPrice = prices[i]; + } else { + maxProfit = Math.max(maxProfit, prices[i] - minPrice); + } + } + return maxProfit; +}; From 8c2011ec52011217115c8d4c7021e2b079086f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tasha=28=EC=82=B4=EB=AF=B8=29?= <45252527+Lustellz@users.noreply.github.com> Date: Sat, 23 Aug 2025 23:14:52 +0900 Subject: [PATCH 2/2] add comments --- best-time-to-buy-and-sell-stock/Lustellz.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/best-time-to-buy-and-sell-stock/Lustellz.ts b/best-time-to-buy-and-sell-stock/Lustellz.ts index 1e86a7a70..b5e0dcf3d 100644 --- a/best-time-to-buy-and-sell-stock/Lustellz.ts +++ b/best-time-to-buy-and-sell-stock/Lustellz.ts @@ -1,5 +1,7 @@ function maxProfit(prices: number[]): number { + // Runtime: 1ms + // Memory: 66.23MB let minPrice: number = prices[0]; let maxProfit: number = 0; @@ -13,4 +15,7 @@ function maxProfit(prices: number[]): number { } } return maxProfit; + + // What I was planning to do: Recursion + // Result: referring other's code };