-
Notifications
You must be signed in to change notification settings - Fork 1
/
16-Maximum-profit.cpp
68 lines (61 loc) · 1.49 KB
/
16-Maximum-profit.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxProfit(int k, int n, int prices[])
{
// Case1: n<=1 or k==0
if(k == 0 || n <= 1)
return 0;
// Case2: 2k >= n
if((2 * k) > n)
{
int ans = 0;
for(int i = 1; i < n; ++i)
{
if(prices[i] > prices[i - 1])
{
ans += (prices[i] - prices[i - 1]);
}
}
return ans;
}
// Case3: 1 <= k < n;
int dp[2 * k + 1];
dp[0] = -prices[0];
for(int i = 1; i <= (2 * k); ++i)
{
if(i % 2 == 0)
dp[i] = INT_MIN;
else
dp[i] = 0;
}
for(int i = 0; i < n; ++i)
{
for(int j = 0; j <= (2 * k); ++j)
{
if(j == 0)
dp[j] = max(dp[j], -prices[i]);
else if(j % 2 == 0)
dp[j] = max(dp[j], dp[j - 1] - prices[i]);
else
dp[j] = max(dp[j], dp[j - 1] + prices[i]);
}
}
return dp[2 * k - 1];
}
};
int main() {
int t;
cin >> t;
while (t--) {
int N, K;
cin >> K;
cin >> N;
int A[N];
for (int i = 0; i < N; i++) cin >> A[i];
Solution ob;
cout << ob.maxProfit(K, N, A) << endl;
}
return 0;
}