Skip to content

Commit 1f47b39

Browse files
author
Partho Biswas
committed
188. Best Time to Buy and Sell Stock IV
1 parent 65af29e commit 1f47b39

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ I have solved quite a number of problems from several topics. See the below tabl
305305
|14| [1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)| [Python](leetcode.com/python/1066_Campus_Bikes_II.py)| | Medium |📌 TODO: Backtracking solution is getting TLE. Solve it and implement it with DP also |
306306
|15| [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Python](leetcode.com/python/121_Best_Time_to_Buy_and_Sell_Stock.py)| [Vid 1](https://codinginterviewclass.com/courses/coding-interview-class/lectures/12305111) | Easy | Fundamental |
307307
|16| [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](leetcode.com/python/122_Best_Time_to_Buy_and_Sell_Stock_II.py)| [Video 01](https://www.youtube.com/watch?v=blUwDD6JYaE)| Easy | Fundamentals |
308-
|17| [123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](leetcode.com/python/123_Best_Time_to_Buy_and_Sell_Stock_III.py)| [Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4) | Hard | Fundamental |
309-
|18| [188. Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [Python](leetcode.com/python/122_Best_Time_to_Buy_and_Sell_Stock_II.py)| --- | Hard | Fundamentals |
308+
|17| [123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](leetcode.com/python/123_Best_Time_to_Buy_and_Sell_Stock_III.py)| **[Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4)** | Hard | Fundamental |
309+
|18| [188. Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [Python](leetcode.com/python/188_Best_Time_to_Buy_and_Sell_Stock_IV.py)| **[Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4)** | Hard | Fundamentals |
310310
|19| [309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| [Python](leetcode.com/python/309_Best_Time_to_Buy_and_Sell_Stock_with_Cooldown.py)| --- | Medium | Fundamental |
311311
|20| [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)| [Python](leetcode.com/python/121_Best_Time_to_Buy_and_Sell_Stock.py)| --- | Medium | Fundamental |
312312

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution(object):
2+
def maxProfit(self, k, prices):
3+
"""
4+
:type k: int
5+
:type prices: List[int]
6+
:rtype: int
7+
"""
8+
return self.maxProfitWithKTransaction(prices, k)
9+
10+
11+
# # This solution is getting "Memory Limit Exceeded"
12+
# def maxProfitWithKTransaction(self, prices, k):
13+
# if not prices:
14+
# return 0
15+
# profits = [[0 for d in prices] for t in range(k + 1)]
16+
# for t in range(1, k + 1):
17+
# maxThusFar = float('-inf')
18+
# for d in range(1, len(prices)):
19+
# maxThusFar = max(maxThusFar, profits[t - 1][d - 1] - prices[d - 1])
20+
# profits[t][d] = max(profits[t][d - 1], maxThusFar + prices[d])
21+
# return profits[-1][-1]
22+
#
23+
#
24+
# Updated implementation to avoid "Memory Limit Exceeded". But yet "Memory Limit Exceeded"
25+
def maxProfitWithKTransaction(self, prices, k):
26+
if not prices or k <= 0:
27+
return 0
28+
evenProfits = [0 for d in prices]
29+
oddProfits = [0 for d in prices]
30+
for t in range(1, k + 1):
31+
maxThusFar = float('-inf')
32+
if t % 2 == 1:
33+
currentProfits = oddProfits
34+
previousProfits = evenProfits
35+
else:
36+
currentProfits = evenProfits
37+
previousProfits = oddProfits
38+
for d in range(1, len(prices)):
39+
maxThusFar = max(maxThusFar, previousProfits[d - 1] - prices[d - 1])
40+
currentProfits[d] = max(currentProfits[d - 1], maxThusFar + prices[d])
41+
return evenProfits[-1] if t % 2 == 0 else oddProfits[-1]
42+
43+
44+
45+
46+
sol = Solution()
47+
# input = [3,2,6,5,0,3]
48+
input = [5,11,3,50,60,90]
49+
k = 2
50+
output = sol.maxProfit(k, input)
51+
print('Res: ', output)

0 commit comments

Comments
 (0)