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#如何引用定义好的dll文件
1 添加引用,找到dll文件 2 引用类的名称空间,生成类的实例,调用类的方法,测试OK.
- js性能优化之函数节流(分流函数)
函数节流的原理 比如我们在window.onresize事件中要打印当前浏览器窗口的大小,在我们通过拖拽来改变窗口大小时候,打印窗口大小这个工作1s就运行了10次.而实际上我们只需要2次或者3次. 比 ...
- poj 1426 Find The Multiple ( BFS+同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18390 Accepted: 744 ...
- <一>读<<大话设计模式>>之简单工厂模式
工厂模式尽管简单.可是写下这篇文章却不简单. 第一:本人经过内心的挣扎后才决定開始写博文的.为什么呢,由于好长时间没有写了,对自己的文学功底也是好不自信.可是技术这东西你不写出来你真不知道自己掌握多少 ...
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
- 【Mongodb教程 第十六课 】 分享NO-SQL开发实战
最近研究了一下NOSQL,现整理目录如下: 一.关系数据库的瓶颈: 二.NOSQL概述: 三.NOSQL中的热门数据库MongoDB介绍及安装配置: 四.MongoDB开发模式及实战: 一.关系数据库 ...
- java实现从报文中获取投保单号
java实现从报文中获取投保单号 投保单号正则表达式: String regex = "<proposalNo>([0-9]+)</proposalNo>[\\s\\ ...
- ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理
ASP.NET没有魔法——ASP.NET MVC Razor与View渲染 对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的 ...
- POJ1195 Mobile phones 【二维树状数组】
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14288 Accepted: 6642 De ...
- Linux文档,目录命令
1,Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同,首先Linux没有"盘(C盘,D盘,E盘的概念)"的概念,已经建立文件系统的硬盘分区被挂载到某 ...