-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimeToBuyAndSellStockIII.py
More file actions
128 lines (94 loc) · 4.22 KB
/
BestTimeToBuyAndSellStockIII.py
File metadata and controls
128 lines (94 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Question link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
# this the solution for the space optimization problem
# Complexity T(N*2*3) SC: O(2*3)
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
if n == 0:
return 0
# # 3D Dp for memoziarion
# dp = [[[0 for _ in range(3)] for _ in range(2)] for _ in range(n + 1)]
# Conver the 3D into 2D
cur = [[0] * 3 for _ in range(2)]
ahead = [[0] * 3 for _ in range(2)]
# Traverse the dp in 3D space
for ind in range(n - 1, -1, -1):
for buy in range(2):
# Cap == 0 is not need to compute
for cap in range(1,3):
# Recurence relations
if buy == 0:
cur[buy][cap] = max(-prices[ind] + ahead[1][cap] , ahead[0][cap])
if buy == 1:
cur[buy][cap] = max(prices[ind] + ahead[0][cap-1] ,ahead[1][cap])
# Upadte the ahead with cur
ahead = [row[:] for row in cur]
# Return the result
return ahead[0][2]
# this is the tabulation problem
# The solution for the problem with complexity T(N*2*3)
# Space Complexity : O(N*2*3)
# def maxProfit(self, prices: List[int]) -> int:
# n = len(prices)
# if n == 0:
# return 0
# # 3D Dp for memoziarion
# dp = [[[0 for _ in range(3)] for _ in range(2)] for _ in range(n + 1)]
# # Traverse the dp in 3D space
# for ind in range(n - 1, -1, -1):
# for buy in range(2):
# # Cap == 0 is not need to compute
# for cap in range(1,3):
# # Recurence relations
# if buy == 0:
# dp[ind][buy][cap] = max(-prices[ind] + dp[ind + 1][1][cap] , dp[ind+1][0][cap])
# if buy == 1:
# dp[ind][buy][cap] = max(prices[ind] + dp[ind + 1][0][cap-1] ,dp[ind+1][1][cap])
# # Return the result
# return dp[0][0][2]
# this is the DP memorization approch With complexity : T(N*)
# TC : O(N*2*3) SC: O(N*2*3) + O(N) for recursion stack
# def maxProfit(self, prices: List[int]) -> int:
# n = len(prices)
# if n == 0:
# return 0
# # 3D Dp for memoziarion
# dp = [[[-1 for _ in range(3)] for _ in range(2)] for _ in range(n)]
# # Recusive function
# def maxProfitOfBTBS(ind,buy,cap,n, prices,dp):
# # Base case
# if ind == n or cap == 0:
# return 0
# # If the value is cahced inside the DP
# if dp[ind][buy][cap] != -1:
# return dp[ind][buy][cap]
# profit = 0
# # Case for the buy
# if buy == 0:
# profit = max(-prices[ind] + maxProfitOfBTBS(ind + 1 ,1,cap,n,prices,dp) , 0 + maxProfitOfBTBS(ind + 1,0,cap,n,prices,dp))
# else:
# profit = max(prices[ind] + maxProfitOfBTBS(ind + 1,0,cap-1,n,prices,dp), 0 + maxProfitOfBTBS(ind + 1,1,cap,n,prices,dp))
# return profit
# #Call the recursive function
# ans = maxProfitOfBTBS(0,0,2,n,prices,dp)
# return ans
# def maxProfit(self, prices: List[int]) -> int:
# n = len(prices)
# if n == 0:
# return 0
# # Complexity TC: O(2^n) SC: O(n)
# # Recusive function
# def maxProfitOfBTBS(ind,buy,cap,n, prices):
# # Base case
# if ind == n or cap == 0:
# return 0
# profit = 0
# # Case for the buy
# if buy == 0:
# profit = max(-prices[ind] + maxProfitOfBTBS(ind + 1 ,1,cap,n,prices) , 0 + maxProfitOfBTBS(ind + 1,0,cap,n,prices))
# else:
# profit = max(prices[ind] + maxProfitOfBTBS(ind + 1,0,cap-1,n,prices), 0 + maxProfitOfBTBS(ind + 1,1,cap,n,prices))
# return profit
# #Call the recursive function
# ans = maxProfitOfBTBS(0,0,2,n,prices)
# return ans