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 ...
随机推荐
- 游戏server设计的一些感悟
Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn.net/chen19870707 Date:September 30 ...
- HDU 1160 FatMouse's Speed(DP)
题意 输入n个老鼠的体重和速度 从里面找出最长的序列 是的重量递增时速度递减 简单的DP 令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度 对与每一个老鼠i 遍历全部老鼠j 当 ...
- centos7 64位系统jdbc连接oracle报错问题
这两天发生了一个错误,记录下来. 报错如下: ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could n ...
- phpqrcode生成带logo的二维码图片及带文字的二维码图片
<?php require_once "./phpqrcode/phpqrcode.php"; /** * 这样就可以生成二维码了,实际上在png这个方法里还有几个参数需要使 ...
- [读书笔记]《没人会告诉你的PPT真相》
这本书分了三部分.第一部分偏重于基础技能,其中分为三部分,打印.放映.保存.第二部分是进阶,分为模板下载.模板修改.增加自定义页面等.第三部分是打造商业范的PPT,分为商业范的特征,具体技能体现(重复 ...
- git unstage
https://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git git rm --c ...
- Masonry 比例(multipliedBy)
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- 并不对劲的cdq分治解三维偏序
为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里-> 参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...
- Django 缓存 使用 Redis Memcached 为网站提速
RedisRedis是一种键值对类型的内存数据库,读写内存比读写硬盘快,我们在Django里面使用Redis非常方便,下面给出详细步骤 基于Ubuntu 1. 安装Redis和django-redis ...
- Redis in python, how do you close the connection?
down voteaccepted Just use redis.Redis. It uses a connection pool under the hood, so you don't have ...