LeetCode 188. Best Time to Buy and Sell Stock IV
来源:互联网 发布:淘宝交易纠纷处理规则 编辑:程序博客网 时间:2024/06/05 09:14
LeetCode 188. Best Time to Buy and Sell Stock IV
题目:
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).
分析:
首先我考虑了动态规划,但是我采用的公式为dp[k][num] =
class Solution {public: int maxProfit(int k, vector<int>& prices) { if (prices.size() <= 1)return 0; else if (k == 0)return 0; else if (k + 1 >= prices.size()) { int sum = 0; for (int index = 0; index < prices.size() - 1; index++) { sum += maxProfit_one(prices, index, index + 1); } return sum; } vector<vector<int>> dp(k, vector<int>(prices.size(),0)); vector<vector<int>> ones(prices.size(), vector<int>(prices.size(), 0)); for (int k_index = 1; k_index <= prices.size() - k; k_index++) for (int index = 0; index < prices.size() - k_index; index++) { ones[index][index + k_index] = maxProfit_one(prices, index, index + k_index); } for (int index = 1; index < prices.size(); index++) { dp[0][index] = ones[0][index]; } for (int num_k = 1; num_k < k; num_k++) for (int index = 2; index < prices.size(); index++) { dp[num_k][index] = max(dp[num_k - 1][index - 1] + ones[index - 1][index],dp[]); //cout << num_k << "," << index << ":" << max_value<<endl; } return dp[k - 1][prices.size()-1]; } //[left,right] int maxProfit_one(vector<int>& prices, int left, int right) { int now_num = prices[left]; int min_num = now_num; int max_money = 0; int money = 0; for (int index = left; index <= right; index++) { if (prices[index] > now_num) { money += (prices[index] - now_num); now_num = prices[index]; } else if (min_num > prices[index]) { max_money = max(max_money, money); money = 0; min_num = prices[index]; now_num = min_num; } else continue; } max_money = max(max_money, money); return max_money; }};
之后我又测试了各种方法,要不出错,要不超时…最后AC的思路如下:
用二维数组dp[i][j]存储第0~j日最多i次操作所得到的最大利润。
用二维数组dps[i][j]存储第0~j日最多i次操作(第2*j次操作为卖出prices[i])所得到的最大利润。
用一维数组ones[i]存储 prices[i+1] > prices[i] ? prices[i+1] - prices[i] : 0
dps[i][j] = max(dps[i][j - 1] + prices[j] - prices[j - 1], dp[i - 1][j - 1] + ones[j - 1]);
dp[i][j] = max(dps[i][j],dp[num_k][i]);
class Solution {public: int maxProfit(int k, vector<int>& prices) { if (prices.size() <= 1)return 0; else if (k == 0)return 0; else if (k + 1 >= prices.size()) { int sum = 0; for (int index = 0; index < prices.size() - 1; index++) { if (prices[index] < prices[index + 1]) sum += prices[index + 1] - prices[index]; } return sum; } vector<vector<int>> dp(k+1, vector<int>(prices.size(),0)); vector<vector<int>> dps(k+1, vector<int>(prices.size(), 0)); vector<int> ones(prices.size() - 1, 0); for (int index = 0; index < prices.size() - 1; index++) { if (prices[index] < prices[index + 1]) ones[index] = prices[index + 1] - prices[index]; } for (int index = 1; index <= k; index++) { dp[index][1] = ones[0]; dps[index][1] = dp[index][1]; } for (int num_k = 1; num_k <= k; num_k++) for (int index = 2; index < prices.size(); index++) { dps[num_k][index] = max(dps[num_k][index - 1] + prices[index] - prices[index - 1], dp[num_k - 1][index - 1] + ones[index - 1]); dp[num_k][index] = max(dps[num_k][index],dp[num_k][index - 1]); } return dp[k][prices.size()-1]; }};
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188. Best Time to Buy and Sell Stock IV
- [LeetCode] 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- 【leetcode】188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- [LeetCode] Best Time to Buy and Sell Stock IV
- leetcode: Best Time to Buy and Sell Stock IV
- LeetCode(188) Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV -- leetcode
- Best Time to Buy and Sell Stock IV--LeetCode
- LeetCode : Best Time to Buy and Sell Stock III & IV
- Best Time to Buy and Sell Stock IV Leetcode Java
- python 3 阿里云python sdk, PyCrypto无法安装解决方法
- ubuntu16.04---解决插入U盘或者移动硬盘后自动弹出文件窗口的问题(取消弹出窗口)
- merge into实现
- C#加载doc文档中的表格与txt文档中的数据然后进行操作
- [Unity][Animation]通过Animator来控制子物体的升起下落
- LeetCode 188. Best Time to Buy and Sell Stock IV
- Elasticsearch与Kibana入门
- java系统高并发解决方案
- 解决springmvc,@Response注解返回json对象出错问题
- 破解教程 第二课 脱壳
- filter interceptor 区别
- ssm(spring,springMvc,mybatis)简易整合空框架
- 编码规范(二)----JAVA编码规范插件
- 企业IT架构转型,你准备好接招了吗?