Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 714. Best Time to Buy and Sell Stock with Transaction Fee
參考資訊:
https://www.cnblogs.com/grandyang/p/7776979.html
題目:

解答:
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
const int SELL = 0;
const int HOLD = 1;
int size = prices.size();
vector<vector<int>> dp(size, vector<int>(2, 0));
dp[0][HOLD] = -prices[0];
for (int i = 1; i < prices.size(); ++i) {
dp[i][SELL] = max(dp[i - 1][SELL], dp[i - 1][HOLD] + (prices[i] - fee));
dp[i][HOLD] = max(dp[i - 1][HOLD], dp[i - 1][SELL] - prices[i]);
}
return dp[size - 1][SELL];
}
};