-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBestTimetoBuyandSellStock-IV_188.cpp
72 lines (48 loc) · 1.39 KB
/
BestTimetoBuyandSellStock-IV_188.cpp
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
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 188. Best Time to Buy and Sell Stock IV
~ Link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
*/
// Approach 1
/*
class Solution {
public:
int dp[101][1001];
int maxProfit(int k, vector<int>& prices) {
int n = prices.size();
if (n <= 0 || k <= 0)
return 0;
for (int i = 1; i <= k; ++i) {
for (int j = 1; j < n; ++j) {
int maxVal = 0;
for (int m = 0; m < j; ++m)
maxVal = max(maxVal, prices[j] - prices[m] + dp[i-1][m]);
dp[i][j] = max(dp[i][j-1], maxVal);
}
}
return dp[k][n-1];
}
};
*/
// T.C. - O(K * N^2)
// S.C. - O(K * N)
// Approach 2 (Efficient)
class Solution {
public:
int dp[101][1001];
int maxProfit(int k, vector<int>& prices) {
int n = prices.size();
if (n <= 0 || k <= 0)
return 0;
for (int i = 1; i <= k; ++i) {
int maxDiff = -prices[0];
for (int j = 1; j < n; ++j) {
dp[i][j] = max(dp[i][j - 1], prices[j] + maxDiff);
maxDiff = max(maxDiff, dp[i-1][j] - prices[j]);
}
}
return dp[k][n-1];
}
};
// T.C. - O(K * N)
// S.C. - O(K * N)