From c16dafc731a0c6302ffbdeb49efe9c7569df086c Mon Sep 17 00:00:00 2001 From: sangminna Date: Fri, 2 May 2025 15:33:52 +0900 Subject: [PATCH] feat: best-time-to-buy-and-sell-stock solution --- best-time-to-buy-and-sell-stock/sm9171.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/sm9171.java diff --git a/best-time-to-buy-and-sell-stock/sm9171.java b/best-time-to-buy-and-sell-stock/sm9171.java new file mode 100644 index 000000000..f39276b5f --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sm9171.java @@ -0,0 +1,13 @@ +class Solution { + public int maxProfit(int[] prices) { + int maxProfit = 0; + int minPrice = prices[0]; + + for (int i = 0; i < prices.length; i++) { + int profit = prices[i] - minPrice; + maxProfit = Math.max(maxProfit, profit); + minPrice = Math.min(minPrice, prices[i]); + } + return maxProfit; + } +}