这是这个系列题目的第四个,题目大意和之前的差不多,但是这次提供最多k次的操作,操作还是不能同时操作即必须结束前一个操作才能进行后一个操作。
状态比较好理解,就是题目要求的缩小版,dp[k][i]表示进行到第i天时最多k次操作能得到的最大利益,但是要注意最后一次操作不一定时第i天完成的。
状态转移方程:dp[k][i] = max(dp[k][i-1],dp[k-1][j]+prices[i]-prices[j]) (0<=j<=i)
比如现在正在考虑dp[k][i],选择有两种,一是第i天不操作,二是在第k-1次第j天后进行第k次操作。这个题的状态转移方程相对来说还是比较容易理解的。
当然如果给的操作数比总天数的一半还要多的话,相邻两天一次交易即可,当然要保证后一天的价格比前一天的价格要高。这种情况就不用再动态的递推了,直接累加即可。
  1. class Solution{
  2. public static int maxProfit(int k,int[] prices) {
  3. int nlen = prices.length;
  4. if(nlen<=1)return 0;
  5. else if(k>nlen/2) {
  6. int res = 0;
  7. for(int i = 1;i<nlen;i++)
  8. if(prices[i]>prices[i-1])res+=(prices[i]-prices[i-1]);
  9. return res;
  10. }else {
  11. int temp = 0;
  12. int[][]dp = new int[k+1][nlen];
  13. Arrays.fill(dp[0],0);
  14. for(int i = 0;i<=k;i++)
  15. dp[i][0] = 0;
  16. for(int kt = 1;kt<=k;kt++) {
  17. for(int i = 1;i<nlen;i++) {
  18. temp = 0;
  19. for(int j = 0;j<=i;j++)
  20. temp = temp>(dp[kt-1][j]+prices[i]-prices[j])?temp:(dp[kt-1][j]+prices[i]-prices[j]);
  21. dp[kt][i] = dp[kt][i-1]>temp?dp[kt][i-1]:temp;
  22. }
  23. }
  24. return dp[k][nlen-1];
  25. }
  26. }
  27. }

不过在最后说一句,由于我编写的代码使用了三层for循环,时间复杂度为O(n^3),在LeetCode上仅仅击败了不到9%的人,额,又是一个比较惨淡的数据了。。。。

动态规划——Best Time to Buy and Sell Stock IV的更多相关文章

  1. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

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

  5. 动态规划——Best Time to Buy and Sell Stock III

    题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益. 示例 1:Input: [3,3,5,0,0,3 ...

  6. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. Lintcode393 Best Time to Buy and Sell Stock IV solution 题解

    [题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...

  8. [LeetCode][Java] Best Time to Buy and Sell Stock IV

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

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

随机推荐

  1. JGUI源码:prefixfree 这个库有时候会引起网页一直加载中(10)

    如题,大部分情况下正常,但是chrome频繁刷新时,会出现这个问题,控制台没有异常信息.最终放弃使用引用第三方库prefixfree.min.js

  2. [转载] win10进行端口转发

    1.添加端口转发netsh interface portproxy add v4tov4 listenport=4000 listenaddress=127.0.0.1 connectport=400 ...

  3. 记我在github上参与的Star增长最快的十万级项目。。。

    前言 GitHub作为程序员的圣地. 用了两三年,一直都觉得,他可以代码托管,项目管理,为项目建立静态主页,个人简历,找工作,面试加分. 然而>>>....昨天才认识到我还是太年轻, ...

  4. PowerPoint 中插入 Latex 公式

    做 PPT 用 Latex Beamer 毕竟还是太麻烦,Beamer 毕竟还是更适合学术性的,各种定义各种公式的那种,遇到要画各种图,插入各种图片,进行错综复杂的排版就比较棘手了. 最终还是 Pow ...

  5. 软件测试面试-如何高质量提交缺陷bug?

    从实际工作中整理,如下:如有补充可以讨论! 所以会发现现在的面试题大部分问的都是工作中出现的场景了,而不是单纯的背诵 1:充分理解需求规则.原型图,知道预期结果,操作时判断是否为bug 解析:预期结果 ...

  6. 查看oracle表空间

    -- 查看oracle表空间 kB, bytes MB, bytes GB from user_segments where segment_type = 'TABLE';

  7. 前端笔记之JavaScript(五)关于数组和字符串那点事

    一.数组 1.1数组概念 数组(array)是一个有序的数据集合.说白了,数组就是一组数.数组内部可以存放一个或多个单独的数据,整体组成数组. 定义数组最简单的方式:数组字面量. 数组的字面量“[]” ...

  8. 如何用java实现一个p2p种子搜索(1)-概念

    前言 说句大实话,网上介绍怎么用java实现p2p种子的搜索这种资料不是特别多,大部分都是python的,用python的话就会简单很多,它里面有很多简单方便的包,libtorrent等等,当然你用这 ...

  9. 【spark】dataframe常见操作

    spark dataframe派生于RDD类,但是提供了非常强大的数据操作功能.当然主要对类SQL的支持. 在实际工作中会遇到这样的情况,主要是会进行两个数据集的筛选.合并,重新入库. 首先加载数据集 ...

  10. 提高github代码下载速度的小技巧

    1.打开如下路径: C:\Windows\System32\drivers\etc 2.将此处的HOSTS文件复制到其他地方,比如桌面.(此处大概率是没有编辑权限的) 3.用记事本打开HOSTS文件, ...