diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.py new file mode 100644 index 0000000000..e6a80e45ea --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.py @@ -0,0 +1,32 @@ +from typing import List + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + # Initialize the minimum price as the price on the first day + min_price = prices[0] + # Initialize max profit as 0 (because we can't make a profit if prices always go down) + max_profit = 0 + + # Loop through the prices starting from the second day + for i in range(1, len(prices)): + # Calculate the profit if we were to sell on this day + profit_today = prices[i] - min_price + # Update max profit if the profit today is higher + max_profit = max(max_profit, profit_today) + # Update min_price to be the lowest price seen so far + min_price = min(min_price, prices[i]) + + return max_profit + +# Example usage +solution = Solution() + +# Example 1 +prices1 = [7, 1, 5, 3, 6, 4] +print("Input:", prices1) +print("Max Profit:", solution.maxProfit(prices1)) # Expected output: 5 + +# Example 2 +prices2 = [7, 6, 4, 3, 1] +print("\nInput:", prices2) +print("Max Profit:", solution.maxProfit(prices2)) # Expected output: 0 (no profit possible) diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/readme.md b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/readme.md new file mode 100644 index 0000000000..cca878d875 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/readme.md @@ -0,0 +1,25 @@ +# Best Time To Buy And Sell Stock + +## Problem Statement +Given an array `prices` where `prices[i]` represents the price of a given stock on the ith day, you need to maximize your profit by selecting a single day to buy the stock and a different day in the future to sell it. + +- The goal is to determine the maximum profit you can achieve from this single transaction. +- If no profit is possible (i.e., prices decrease continuously), return 0. + +### Example +1. **Input**: `prices = [7, 1, 5, 3, 6, 4]` + - **Output**: `5` + - **Explanation**: Buy on day 2 (price = 1) and sell on day 5 (price = 6), achieving a maximum profit of `6 - 1 = 5`. + +2. **Input**: `prices = [7, 6, 4, 3, 1]` + - **Output**: `0` + - **Explanation**: No profit can be made, as prices are in descending order. + +## Solution Approach +The solution involves a single-pass algorithm to find the maximum profit efficiently. + +1. **Track Minimum Price**: Keep track of the lowest stock price encountered as you iterate over the array. This is the day to "buy" for maximum profit. +2. **Calculate Maximum Profit**: For each price, calculate the potential profit if you were to sell on that day by subtracting the minimum price seen so far. +3. **Update Maximum Profit**: If the potential profit for the current price is greater than the maximum profit so far, update the maximum profit. + +This approach has a time complexity of **O(n)**, as we only traverse the `prices` array once. diff --git a/Project-Structure.md b/Project-Structure.md index 1bf0d8336d..dd74122d37 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -81,6 +81,8 @@ * [Test Main](Algorithms_and_Data_Structures/Dijkstra/test_main.py) * Dynamic-Programming-Series * Basic-Dp-Problems + * Best-Time-To-Buy-And-Sell-Stock + * [Program](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.py) * [Climbing-Stairs](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py) * [Fibonacci-Seq](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py) * [House-Robber](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py)