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. 特殊js事件

    1:点击enter事件 $(document).keypress(function(e) { // 回车键事件 if(e.which == 13) { submitForm(); } }); 2:JQ ...

  2. php工作笔记8-并发和数据类型

    1.mysql在进行数据的修改时,并发情况下: $RoundsRows=$modelRounds->where("id=$roundsID and (sendMoney + $amou ...

  3. 5.openssl dgst

    该伪命令用于生成文件的信息摘要,也可以进行数字签名,验证数字签名. 首先要明白,要进行数字签名,需要计算出特征码即数字摘要,然后使用私钥对数字摘要进行签名.特征码使用md5,sha等计算出. 对象只能 ...

  4. STP的作用和操作

    STP的作用 STP通过阻塞端口来消除环路,并能够实现链路备份的目的 STP的操作 选举一个根桥 比较交换机的桥ID,越小越优先 桥ID  是8个字节,2个字节的优先级+6个字节的MAC地址 2.每个 ...

  5. 使用js脚本批量下载慕课网视频

    慕课网(http://www.imooc.com/)上有很多不错的视频,当然我不是来给慕课网打广告的,我本人学习过很多慕课网上的免费的视频. 在线看如果网速慢时,可能会有卡顿,没网时无法观看.所有说下 ...

  6. bootstrap style for jQuery UI Dialog

    页面引用: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  7. Spark-Mllib(二)基本统计

    一.基本统计量 统计向量的长度,最大值,最小值,非0个数,模1和模2,方差等 import org.apache.spark.mllib.linalg.{Vector,Vectors} import ...

  8. 【转】解决IIS7该问.svc文件的错误问题

        解决IIS7.5中部署WCF时,访问.svc文件的404错误问题如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误.解决方法,以管理员身份进入命令行模式,运行:" ...

  9. ie 出现 append无效

    今天发现用ie append  无效,但是在谷歌浏览器上可以使用,问题在于 拼接字符串的时候出现多了一个标签. 解决方法:检查是否有多余或少写html标签.

  10. DOS命令(可查看本机IP地址各个网卡号)

    网卡号指的是网卡的编号,也就是网卡的物理地址.查看方法:(以win7为例)打开开始 输入cmd 回车后进入dos命令行模式,然后输入ipconfig /all 回车查看结果找到"无线局域网适 ...