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.

解题思路:

https://leetcode.com/discuss/18330/is-it-best-solution-with-o-n-o-1%E3%80%82

本题是Best Time to Buy and Sell Stock系列最难的一道,

参考Java for LeetCode 123 Best Time to Buy and Sell Stock III解法二的思路,四个数组可以压缩为两个数组,JAVA实现如下:

  1. public int maxProfit(int k, int[] prices) {
  2. if (k == 0 || prices.length < 2)
  3. return 0;
  4. if (k > prices.length / 2) {
  5. int maxProfitII = 0;
  6. for (int i = 1; i < prices.length; ++i)
  7. if (prices[i] > prices[i - 1])
  8. maxProfitII += prices[i] - prices[i - 1];
  9. return maxProfitII;
  10. }
  11. int[] buy=new int[k];
  12. int[] sell=new int[k];
  13. for(int i=0;i<buy.length;i++)
  14. buy[i]=Integer.MIN_VALUE;
  15. for (int i = 0; i < prices.length; i++)
  16. for (int j = k - 1; j >= 0; j--) {
  17. sell[j] = Math.max(sell[j], buy[j] + prices[i]);
  18. if (j == 0)
  19. buy[j] = Math.max(buy[j], -prices[i]);
  20. else
  21. buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
  22. }
  23. return sell[k - 1];
  24. }

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】的更多相关文章

  1. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

    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 买卖股票的最佳时间 IV

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

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

  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. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

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

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

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

  8. Java for LeetCode 122 Best Time to Buy and Sell Stock II

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

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

随机推荐

  1. 【HDU 2546】饭卡(DP+贪心)

    贪心:最贵的留到最后买.状态转移方程:dp[j]=dp[j+a[i]]|dp[j],dp[i]表示余下i元. 原来就不足5元,那就不能买啦. #include<cstdio> #inclu ...

  2. Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析

    上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...

  3. BZOJ-1192 鬼谷子的钱袋 2^n有关数论

    1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2473 Solved: 1806 [Submit][St ...

  4. 装了虚拟机,但是没有虚拟网卡vmnet0 vmnet1 vmnet8

    在服务里面启动图中的两个服务,在再你的虚拟网络编辑器里面点恢复默认设置就会出现了,我的是win8.1,默认这两个服务是手动启动的,可能是优化软件优化的结果 我是win10  里面显示以太网3 和 4, ...

  5. Linux System Log Collection、Log Integration、Log Analysis System Building Learning

    目录 . 为什么要构建日志系统 . 通用日志系统的总体架构 . 日志系统的元数据来源:data source . 日志系统的子安全域日志收集系统:client Agent . 日志系统的中心日志整合系 ...

  6. c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常

    #include"stdafx.h" #include"string" #include<iostream> #include<vector& ...

  7. 项目总结—jQuery EasyUI- DataGrid使用

    http://blog.csdn.net/zwk626542417/article/details/18839349 概要 jQuery EasyUI是一个基于jquery的集成了各种用户界面的框架, ...

  8. c++模板库(简介)

    目 录 STL 简介 ......................................................................................... ...

  9. 集成学习原理:Adaboost

    集成学习通过从大量的特征中挑出最优的特征,并将其转化为对应的弱分类器进行分类使用,从而达到对目标进行分类的目的. 核心思想 它是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器), ...

  10. Facebook内部高效工作PPT指南

    Facebook内部高效工作PPT指南 Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下25张PP ...