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 ...
随机推荐
- win8 metro 自己写摄像头录像项目
这是要求不适用CameraCaptureUI等使用系统自带的 camera UI界面.要求我们自己写调用摄像头摄像的方法,如今我把我的程序贴下: UI界面的程序: <Page x:Class= ...
- Dubbo应用启动与停止脚本,超具体解析
本周刚好研究了一下dubbo的启动脚本,所以在官网的启动脚本和公司内部的启动脚本做了一个整理,弄了一份比較通过的Dubbo应用启动和停止脚本. 以下的脚本仅仅应用于配置分离的应用.什 ...
- 一个有意思的Ruby Webdriver超时问题的解决过程
rescue in receive 由于写ruby的时候感觉混身上下都拽起来了,所以比較喜欢用ruby写代码. 今天遇到了一个webdriver timeout的问题,问题本身还是由于我对webdri ...
- ZOJ 3691 Flower(最大流+二分)
Flower Time Limit: 8 Seconds Memory Limit: 65536 KB Special Judge Gao and his girlfriend's ...
- C项目实践--家庭财务管理系统
1.功能需求分析 家庭财务管理系统给家庭成员提供了一个管理家庭财务的平台,系统可以对家庭成员的收入和支出进行增加,删除.修改和查询等操作,并能统计总收入和总支出.其主要功能需求描述如下: (1)系统主 ...
- solr入门之多线程操作solr中索引字段的解决
涉及的问题: 建索引时有一个字段是该词语出现的次数,这个字段是放在solr里的 而我用的是多线程来进行全量导入的,这里就涉及到了多线程问题 多个线程操作同一个变量时怎样处理? 我是这样子做的 : 首 ...
- mysql04--存储过程
过程:若干语句,调用时执行封装的体.没有返回值的函数. 函数:是一个有返回值的过程 存储过程:把若干条sql封装起来,起个名字(过程),并存储在数据库中. 也有不存储的过程,匿名过程,用完就扔(mys ...
- Ubuntu 12.10终端Terminal快捷方式调用
1:使用快捷键:ctrl+alt+t 打开终端 2:在终端上右键,选“Lock to launcher” 这样就锁定在左侧了,需要用时,直接点就打开了.
- YTU 1098: The 3n + 1 problem
1098: The 3n + 1 problem 时间限制: 1 Sec 内存限制: 64 MB 提交: 368 解决: 148 题目描述 Consider the following algor ...
- 【183】VMware + CentOS 安装教程等
目录: VMware 虚拟机安装 虚拟机上安装 CentOS VMware Tools 安装 为程序增加环境变量 其他 1. VMware 虚拟机安装 ※ 参考:VMware 虚拟机安装使用教程( ...