Skip to content

Commit

Permalink
Merge pull request #944 from SKG24/main
Browse files Browse the repository at this point in the history
Closes #932 : Addition of Dynamic Prog | Best time to Buy and Sell Stock
  • Loading branch information
UTSAVS26 authored Oct 30, 2024
2 parents 7551348 + a2d8227 commit 70d2c7a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 70d2c7a

Please sign in to comment.