188. Best Time to Buy and Sell Stock IV leetcode解题笔记
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).
这一题比上一题出现的变化 为 限制不再为两次 而是K次 再一次增加了难度
因为上一题的原因 这里想到了常用的一种算法 动态规划 虽然上一题的动态规划很抽象 但是这里我们具体化一点
首先我们的动态方程怎么设计 根据要求
能不能用一个二维数组profit[t,i]表示 通过T次交易 在第I个商品能获得的最大利润 那么profit[k,n]就是在第N个商品通过K次交易能获得的最大利润
根据推理 得出下列方程
profit[t,i]=max(profit(t,i-1),prices[i]+tmp)
tmp=max(tmp,profit(t-1,i-1)-prices[i])
tmp初始化为第一个商品的价格
这里解释一下 tmp的方程怎么来的 profit(t-1,i-1)-prices[i]表明 在第i-1个商品通过t-1次交易获得利润后 再买入第i个商品 并且跟之前的tmp比较取最大值
profit[t,i]中prices[i]+tmp 表明在之前的tmp基础上 卖出第I个商品获得的利润 和除去第I个商品获得的利润作比较 最大值
同时我们要知道K次是用户自定的 这里有一种特殊情况 我们买东西和卖东西就是两次动作 假设数组有四个数 我们最多进行两次交易 也就是4/2 假设用户给定K大于4/2 就回到了之前我们解决的第二个问题 不限定交易次数 获得最大交易值
这种特殊情况显然不能用动态方程 先除去这种情况 再用动态方程求解
有了思路 开始码代码
public class Solution {
public int maxProfit(int k, int[] prices) {
if(k>prices.length/2)
return inmaxProfit(prices);
int profit[][] =new int[k+1][prices.length];
for(int i=1;i<=k;i++){
int tmp=-prices[0];
for(int j=1;j<prices.length;j++){
profit[i][j]=Math.max(profit[i][j-1],prices[j]+tmp);
tmp=Math.max(tmp,profit[i-1][j-1]-prices[j]);
}
}
return profit[k][prices.length-1];
}
public int inmaxProfit(int[] prices){
int profit=0;
for(int i=0;i<prices.length-1;i++){
int diff=prices[i+1]-prices[i];
if(diff>0){
profit++;
}
}
return profit;
}
}
提交

看看哪里出了问题
给出的K是2 大于三个数的一半 所以进入的是第二个函数
profit++ 错了 应该是profit+=diff 修改 提交
public class Solution {
public int maxProfit(int k, int[] prices) {
if(k>prices.length/2)
return inmaxProfit(prices);
int profit[][]=new int[k+1][prices.length];
for(int i=1;i<=k;i++){
int tmp=-prices[0];
for(int j=1;j<prices.length;j++){
profit[i][j]=Math.max(profit[i][j-1],prices[j]+tmp);
tmp=Math.max(tmp,profit[i-1][j-1]-prices[j]);
}
}
return profit[k][prices.length-1];
}
public int inmaxProfit(int[] prices){
int profit=0;
for(int i=0;i<prices.length-1;i++){
int diff=prices[i+1]-prices[i];
if(diff>0){
profit+=diff;
}
}
return profit;
}
}

成功
188. Best Time to Buy and Sell Stock IV leetcode解题笔记的更多相关文章
- 188. Best Time to Buy and Sell Stock IV——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 188. Best Time to Buy and Sell Stock IV (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
随机推荐
- python迭代器,生成器,装饰器,context模块
迭代器iteration 是访问集合元素的一种方式,只能往前不能往后迭代器的特点:1,访问者不需要关注迭代器内部结构,只需通过next()不断取下一个内容2,访问不能回退3,循环较大数据集合时,省内存 ...
- 重走java--Step 2
java基础语法(一) 1.java基本数据类型 byte:1个字节,8bit有符号数据类型boolean:2个字节,16bit布尔数值char:2个字节,16bit的Unicode字符类型int:4 ...
- 第五章 征服数据库(Spring对DB的使用)——开发持久层
本章内容: 定义Spring对数据库访问的支持 配置数据库资源 使用Spring的JDBC模板 在几乎所有的企业级应用中,都需要构建数据持久层.现在意义上的数据持久层是指把对象或者数据保存到数据库中, ...
- 用VC进行COM编程所必须掌握的理论知识
一.为什么要用COM 软件工程发展到今天,从一开始的结构化编程,到面向对象编程,再到现在的COM编程,目标只有一个,就是希望软件能象积方块一样是累起来的,是组装起来的,而不是一点点编出来的.结构化编程 ...
- Unity 几种优化建议
转: http://user.qzone.qq.com/289422269/blog/1453815561?ptlang=2052 Unity 几种优化建议 最简单的优化建议: 1.PC平台的话保持场 ...
- flash开发几个问题
1.跨域请求xml或txt 问题,使用URLLoader,不能跨域使用 2.js和swf交互问题,尽量使用swfobject.js动态创建
- 四元数quaternion
四元数的简单方法运用四元数在Unity3D中的作用就是拿来表示旋转. AngleAxis 创建一个旋转,绕着某个轴旋转,返回结果是一个四元数. 跟ToAngleAxis实现的是相反的功能. Angle ...
- Android菜鸟成长记9 -- selector的用法
在项目开发的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片.开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点 ...
- 【Python全栈笔记】08 [模块二] 20 Oct 递归 -*** 待补充
递归 引入 递归的表现形式 下面是四个函数,互相调用返回结果 # 引入 递归的表现形式 def f1(): ' def f2(): r = f1() return r def f3(): r = f2 ...
- ef 对象无法序列化的问题(System.Data.Entity.DynamicProxies)
错误提示: System.InvalidOperationException: 生成 XML 文档时出错. ---> System.InvalidOperationException: 不应是类 ...