## 188. Best Time to Buy and Sell Stock IV 原

cofama

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

``````class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(prices.empty()) return 0;
if(prices.size()<=k*2) {
int profit=0;
for(int i=1; i<prices.size(); i++) {
if(prices[i]>prices[i-1]) {
profit += prices[i]-prices[i-1];
}
}
return profit;
}

int dp[prices.size()][k+1];
for(int i=0; i<prices.size(); i++)
dp[i][0]=0;
for(int j=1; j<=k; j++)
dp[0][j]=0;

for(int j=1; j<=k; j++) {
for(int i=1; i<prices.size(); i++) {
if(prices[i]>prices[i-1]) {
}
else {
dp[i][j] = dp[i-1][j];
}
}
}

return dp[prices.size()-1][k];
}
};
``````

### cofama

Leetcode 121. Best Time to Buy and Sell Stock

SnailTyan
2018/10/19
0
0
Leetcode-Easy 121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock 描述： 思路： [1,2,3,4] ==> returns 3 (buy at 1 and sell at 4) [4,3,2,1] ==> returns 0 (don't buy) [4,10,25,2,10] ==> returns 21 (buy at 4 a......

2018/04/01
0
0
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (在具有冻结时间条件下买入和卖出股票的最佳时间)

dby_freedom
2018/12/06
0
0
Best Time To Sell Stock 3

Finley.Hamilton
2014/12/04
0
0
Leetcode 122. Best Time to Buy and Sell Stock II

SnailTyan
2018/10/30
0
0

Java B2B2C o2o多用户商城 springcloud架构-docker-feign-hystrix（六）

sccspuercode
28分钟前
2
0

linuxCool
47分钟前
2
0
C++ This 详解

3
0

CodeSheep

1
0

em_aaron

4
0