188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格。
设计一个算法来找到最大的利润。您最多可以完成 k 笔交易。
注意:
你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
详见:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/
Java实现:
class Solution {
public int maxProfit(int k, int[] prices) {
int n=prices.length;
if(n==0||prices==null){
return 0;
}
if(k>=n){
int res=0;
for(int i=1;i<n;++i){
if(prices[i]-prices[i-1]>0){
res+=prices[i]-prices[i-1];
}
}
return res;
}
int[] g=new int[k+1];
int[] l=new int[k+1];
for(int i=0;i<n-1;++i){
int diff=prices[i+1]-prices[i];
for(int j=k;j>=1;--j){
l[j]=Math.max(g[j-1]+Math.max(diff,0),l[j]+diff);
g[j]=Math.max(g[j],l[j]);
}
}
return g[k];
}
}
C++实现:
class Solution {
public:
int maxProfit(int k, vector<int> &prices) {
if (prices.empty())
{
return 0;
}
if (k >= prices.size())
{
return solveMaxProfit(prices);
}
int g[k + 1] = {0};
int l[k + 1] = {0};
for (int i = 0; i < prices.size() - 1; ++i)
{
int diff = prices[i + 1] - prices[i];
for (int j = k; j >= 1; --j)
{
l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff);
g[j] = max(g[j], l[j]);
}
}
return g[k];
}
int solveMaxProfit(vector<int> &prices) {
int res = 0;
for (int i = 1; i < prices.size(); ++i)
{
if (prices[i] - prices[i - 1] > 0)
{
res += prices[i] - prices[i - 1];
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4295761.html
188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV的更多相关文章
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 3.Best Time to Buy and Sell Stock(买卖股票)
Level: Easy 题目描述: Say you have an array for which the ith element is the price of a given stock ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- C语言必会面试题(3、耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)
3.耶稣有13个门徒.当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1.2,3.1,2,3.... 凡是报到"3"就退出圈子.最后留在圈子 ...
- ReLu(修正线性单元)、sigmoid和tahh的比较
不多说,直接上干货! 最近,在看论文,提及到这个修正线性单元(Rectified linear unit,ReLU). Deep Sparse Rectifier Neural Networks Re ...
- scikit-learn:class and function reference(看看你究竟掌握了多少。。)
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.decomposition Reference This is t ...
- Random-随机生成电话号
package test1; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; ...
- 【大数据project师之路】Hadoop——MapReduce概述
一.概述. MapReduce是一种可用于数据处理的编程模型.Hadoop能够执行由各种语言编写的MapReuce程序.MapReduce分为Map部分和Reduce部分. 二.MapReduce的机 ...
- ExtJs里表格自动显隐滚动条
ExtJs里面,layout:'border'这种布局应该很常用,但我用的时候,因为不熟,走了一些弯路.比如说,一个页面,大体布局是这样的: 上:查询输入框 中+下:查询结果(表格,底部有分页控件) ...
- struts2的(S2-045,CVE-2017-5638)漏洞测试笔记
网站用的是struts2 的2.5.0版本 测试时参考的网站是http://www.myhack58.com/Article/html/3/62/2017/84026.htm 主要步骤就是用Burp ...
- java语法基础(二)
流程控制语句 表达式语句 在表达式后面添加:就构成了表达式语句,简称“语句” 我们编写java代码,更多时候都是在书写表达式语句. int i;声明语句 i = 10;赋值语句 流程控制语句 流程控制 ...
- 连通图(Tarjan算法) 专题总结
一.题目类型: 1.有向图的强连通分量: POJ1236 Network of Schools HDU1269 迷宫城堡 2.割点 & 割边: UESTC - 900 方老师炸弹 UVA315 ...
- HDU - 2066 一个人的旅行(最短路径)(模板)
d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到这个城市的距离设为0),草儿想去的地方有D个: 求D个城市中距离草儿家最近的距离. s.进行1次单源最短路,找出 ...