Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum profit we can make after k Buy and Sell in python
Stock trading problems are common in dynamic programming. Given a list of stock prices and a maximum number of transactions k, we need to find the maximum profit possible. Each transaction consists of buying and then selling a stock.
So, if the input is like prices = [7, 3, 5, 2, 3] and k = 2, then the output will be 3. We can buy at price 3, sell at 5 (profit = 2), then buy at 2 and sell at 3 (profit = 1), giving us total profit of 3.
Approach
We'll use dynamic programming with recursion. The key insight is to track three states:
- Current position in the price array
- Number of transactions remaining
- Whether we currently hold a stock
Algorithm Steps
The recursive function dp(i, k, bought) works as follows ?
- If we reach the end of prices or have no transactions left, return 0
- If we currently hold stock (
bought = True), we can either:- Sell it: get
prices[i]profit and decrease transaction count - Hold it: move to next day without selling
- Sell it: get
- If we don't hold stock (
bought = False), we can either:- Buy it: pay
prices[i]cost - Skip it: move to next day without buying
- Buy it: pay
Example
class Solution:
def solve(self, prices, k):
def dp(i, k, bought):
if i == len(prices) or k == 0:
return 0
if bought:
# We can sell (complete transaction) or hold
return max(dp(i + 1, k - 1, False) + prices[i], dp(i + 1, k, bought))
else:
# We can buy or skip
return max(dp(i + 1, k, True) - prices[i], dp(i + 1, k, bought))
return dp(0, k, False)
# Test the solution
ob = Solution()
prices = [7, 3, 5, 2, 3]
k = 2
result = ob.solve(prices, k)
print(f"Maximum profit with {k} transactions: {result}")
Maximum profit with 2 transactions: 3
How It Works
For prices = [7, 3, 5, 2, 3] and k = 2:
- Buy at index 1 (price 3), sell at index 2 (price 5) ? profit = 2
- Buy at index 3 (price 2), sell at index 4 (price 3) ? profit = 1
- Total profit = 2 + 1 = 3
Optimized Version with Memoization
class Solution:
def solve(self, prices, k):
memo = {}
def dp(i, k, bought):
if (i, k, bought) in memo:
return memo[(i, k, bought)]
if i == len(prices) or k == 0:
return 0
if bought:
result = max(dp(i + 1, k - 1, False) + prices[i], dp(i + 1, k, bought))
else:
result = max(dp(i + 1, k, True) - prices[i], dp(i + 1, k, bought))
memo[(i, k, bought)] = result
return result
return dp(0, k, False)
# Test with memoization
ob = Solution()
prices = [1, 5, 3, 6, 4]
k = 2
result = ob.solve(prices, k)
print(f"Maximum profit: {result}")
Maximum profit: 7
Conclusion
This dynamic programming approach efficiently solves the k-transaction stock problem by exploring all possible buy/sell decisions. Memoization improves performance by avoiding redundant calculations, making it suitable for larger inputs.
