From d7e31629884a4184a9350072bc6e6ee8ffbf3218 Mon Sep 17 00:00:00 2001 From: Gurudevkini Date: Sun, 26 Oct 2025 23:55:28 +0530 Subject: [PATCH] Add solution for Best Time to Buy and Sell Stock Implemented a solution to calculate maximum profit from stock prices. --- 2. Best Time to Buy and Sell Stock (Easy) | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 2. Best Time to Buy and Sell Stock (Easy) diff --git a/2. Best Time to Buy and Sell Stock (Easy) b/2. Best Time to Buy and Sell Stock (Easy) new file mode 100644 index 000000000..4a4558383 --- /dev/null +++ b/2. Best Time to Buy and Sell Stock (Easy) @@ -0,0 +1,13 @@ +2. Best Time to Buy and Sell Stock (Easy) + +Problem: Max profit from one buy-sell transaction. + class Solution { + public int maxProfit(int[] prices) { + int min = prices[0], profit = 0; + for (int p : prices) { + min = Math.min(min, p); + profit = Math.max(profit, p - min); + } + return profit; + } +}