-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path188-kprofit.java
More file actions
25 lines (24 loc) · 829 Bytes
/
188-kprofit.java
File metadata and controls
25 lines (24 loc) · 829 Bytes
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
import java.util.*;
class Solution_188 {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if(len == 0) return 0;
if(k>len/2) { // 可以无限交易,贪心算法
int res = 0;
for(int i=0; i<len-1; i++) {
if(prices[i+1] > prices[i]) res+=prices[i+1]-prices[i];
}
return res;
}
int[] with = new int[k+1]; // 第i天手上有股票
int[] without = new int[k+1];
Arrays.fill(with, -Integer.MAX_VALUE);
for(int i=0; i<len; i++) { //状态法
for(int j=1; j<k+1; j++) {
with[j] = Math.max(with[j], without[j-1]-prices[i]);
without[j] = Math.max(without[j], with[j]+prices[i]);
}
}
return without[k];
}
}