-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #944 from SKG24/main
Closes #932 : Addition of Dynamic Prog | Best time to Buy and Sell Stock
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...s/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
25 changes: 25 additions & 0 deletions
25
...-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters