I don't know why it happens.

I used the way of DP in O(kn) complexity, and local test is OK, but online test time limit exceed.

code is as follows :

public int maxProfit(int k, int[] prices) {

int len = prices.length;

```
if (k > len) {
k = len;
}
int[][] profit = new int[k][2];
int[] buy = new int[k];
if (k < 1 || len < 1) {
return 0;
}
for (int i=0; i<k; i++) {
profit[i][0] = 0;
profit[i][1] = 0;
buy[i] = prices[0];
}
for (int i=1; i<len; i++) {
for (int j=1; j<k; j++) {
profit[j][0] = Math.max(profit[j][0], profit[j-1][1] + prices[i] - buy[j]);
if (profit[j][1] < profit[j-1][0]) {
profit[j][1] = profit[j-1][0];
buy[j] = prices[i];
}
}
}
return Math.max(profit[k-1][0], profit[k-1][1] + prices[len-1] - buy[k-1]);
}
```