From 8cef9207d8351e24dd6815d3160cc27fa99cb470 Mon Sep 17 00:00:00 2001 From: Hyoga Date: Fri, 7 Nov 2025 23:44:55 +0900 Subject: [PATCH] Implement maxProfit function for stock prices --- .../20251107.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/leetcode/121_best-time-to-buy-and-sell-stock/20251107.ts diff --git a/src/leetcode/121_best-time-to-buy-and-sell-stock/20251107.ts b/src/leetcode/121_best-time-to-buy-and-sell-stock/20251107.ts new file mode 100644 index 0000000..826cbc2 --- /dev/null +++ b/src/leetcode/121_best-time-to-buy-and-sell-stock/20251107.ts @@ -0,0 +1,16 @@ +function maxProfit(prices: number[]): number { + const n = prices.length + let maxProfit = 0; + + for (let i = 0; i < n - 1; i++) { + for (let j = i + 1; j < n; j++) { + const todayProfit = prices[j] - prices[i]; + + if (maxProfit < todayProfit) { + maxProfit = todayProfit; + } + } + } + + return maxProfit; +};