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解题笔记的更多相关文章

  1. 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 ...

  2. 【刷题-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 ...

  3. 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 ...

  4. [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 ...

  5. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 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 ...

  7. 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 ...

  8. 188. Best Time to Buy and Sell Stock IV

    题目: 链接: 题解: 测试: Reference:

  9. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

随机推荐

  1. arcgis制作风或水流速流向图

    制作风或水流速流向图 风速风向图或流速流向图相信大家都已经见过不少,但不知道有多少人会制作这样炫的专题图,下面这边文章向我们展示了当基本数据U和V矢量被存储时,怎样计算风或水流的速度和方向和对其进行符 ...

  2. win10 自动亮度关闭无效问题

    升级win10后,发现系统的关闭自动亮度功能无效了,怎么调整都没有效果,百度把,服务也关了,电源管理也关了,自己的显卡节电设置也关了,最后摸索,只要把电池状态下,和通电状态下的屏幕亮度都调整到50%以 ...

  3. [转]非常实用的15款开源PHP类库

    源文件:http://www.csdn.net/article/2013-10-09/2817123-PHP-Libraries 英文原文:https://codegeekz.com/useful-p ...

  4. Node入门(转)

    原文链接:http://www.nodebeginner.org/index-zh-cn.html Node入门 作者: Manuel Kiessling翻译: goddyzhao & Gra ...

  5. 关于C转汇编(转自网上)

    ②在KILE软件的菜单中,选择Project-->Options for  Target 'Target 1',-->Listing选择Assembly code就能生产*.LST文件.在 ...

  6. [ZT]嵌入视频播放器代码

    http://www.cnblogs.com/liulanglang/archive/2007/11/29/976638.html 页面插入REAL播放器代码: < id=video1 styl ...

  7. 16.2.2 Space Needed for keys

    myisam表使用btree索引,可以粗略计算出索引文件的大小,使用(key_length+4)/0.67,全部key的总和,全部key被排序顺序插入和表没有被任何压缩的时候,这是最坏的情况 stri ...

  8. Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例

    遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例: #-*- encoding: utf-8 -*- __author__ = 'liudong' import linecache,sys ...

  9. mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command

    问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt]   报错:sed: 1: "test.txt": undefined label 'est.txt' ...

  10. Win10怎么设置始终以管理员身份运行应用程序

    第一步.对着Win10应用程序桌面图标单击鼠标右键,菜单中选择“属性”; 第二步.在程序属性对话框,点击切换到“兼容性”选项卡,找到并勾选“设置”项目下的“以管理员身份运行此程序”后,点击确定即可. ...