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

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example 1:

  1. Input: [2,4,1], k = 2
  2. Output: 2
  3. Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

  1. Input: [3,2,6,5,0,3], k = 2
  2. Output: 7
  3. Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
  4.   Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
  5.  
  6. 使用动态规划
  1. public int maxProfit(int k, int[] prices) {//dp mytip
  2. if(null==prices||0==prices.length||0>=k){
  3. return 0;
  4. }
  5. int max =0;
  6. if(k>prices.length){//若k过大 优化
  7. for (int i = 0; i < prices.length-1; i++) {
  8. if(prices[i]<prices[i+1]){
  9. max+= prices[i+1]-prices[i];
  10. }
  11. }
  12. }
  13. else{
  14. int[][][] states = new int[prices.length][k+1][2];//状态表示第i个数在第j此交易中,有无股票时(0为无,1为有)的利益;//因为只保存上一个数时的利益,所以states可优化为[k+1][2]
  15. for(int i=0;i<=k;i++){//初始化第1个数的状态
  16. states[0][i][1]=-prices[0];
  17. }
  18. for(int i=1;i<prices.length;i++){
  19. for(int j=0;j<=k;j++){
  20. if(j==0){
  21. states[i][j][0] = states[i-1][j][0];//防止j-1溢出
  22. }
  23. else{
  24. states[i][j][0] = Math.max(states[i-1][j][0],states[i-1][j-1][1]+prices[i]);
  25. }
  26. states[i][j][1] = Math.max(states[i-1][j][1],states[i-1][j][0]-prices[i]);
  27. }
  28. }
  29. for(int i=0;i<=k;i++){
  30. max = max>states[prices.length-1][i][0]?max:states[prices.length-1][i][0];
  31. }
  32. }
  33.  
  34. return max;
  35. }

优化空间

  1. public int maxProfit(int k, int[] prices) {//dp my
  2. if(null==prices||0==prices.length||0>=k){
  3. return 0;
  4. }
  5. int max =0;
  6. if(k>prices.length){
  7. for (int i = 0; i < prices.length-1; i++) {
  8. if(prices[i]<prices[i+1]){
  9. max+= prices[i+1]-prices[i];
  10. }
  11. }
  12. }
  13. else{
  14. int[][] states = new int[k+1][2];
  15. states[0][1] = -prices[0];
  16. for(int i=0;i<=k;i++){
  17. states[i][1]=-prices[0];
  18. }
  19. for(int i=1;i<prices.length;i++){
  20. for(int j=0;j<=k;j++){
  21. states[j][1] = Math.max(states[j][1],states[j][0]-prices[i]);
  22. if(j==0){
  23. states[j][0] = states[j][0];
  24. }
  25. else{
  26. states[j][0] = Math.max(states[j][0],states[j-1][1]+prices[i]);
  27. }
  28.  
  29. }
  30. }
  31. for(int i=0;i<=k;i++){
  32. max = max>states[i][0]?max:states[i][0];
  33. }
  34. }
  35.  
  36. return max;
  37. }

相关题

买卖股票的最佳时间1 LeetCode121 https://www.cnblogs.com/zhacai/p/10429264.html

买卖股票的最佳时间2 LeetCode122 https://www.cnblogs.com/zhacai/p/10596627.html

买卖股票的最佳时间3 LeetCode123 https://www.cnblogs.com/zhacai/p/10645571.html

买卖股票的最佳时间冷冻期 LeetCode309 https://www.cnblogs.com/zhacai/p/10655970.html

买卖股票的最佳时间交易费 LeetCode714 https://www.cnblogs.com/zhacai/p/10659288.html

LeetCode-188.Best Time to Buy and Sell Stock IV的更多相关文章

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

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

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

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

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

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

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

  10. 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. Java如何清除空格?

    在Java编程中,如何清除/删除空格? 以下示例演示如何使用Util.regex.Pattern类的matcher.replaceAll(stringname)方法来删除空格. package com ...

  2. Linux系统排查4——网络篇

    用于排查Linux系统的网络故障. 网络排查一般是有一定的思路和顺序的,其实排查的思路就是根据具体的问题逐段排除故障可能发生的地方,最终确定问题. 所以首先要问一问,网络问题是什么,是不通,还是慢? ...

  3. Memcached 总结 启动多个Memcached服务 配置文件详解

    一. 1.解压下载的安装包到指定目录. 2.服务安装,使用管理员权限运行以下命令: c:\memcached\memcached.exe -d install 二.同一台Windows机器中启动多个M ...

  4. mysql数据库建立的数据库在哪个文件夹?

    一般在安装目录下的data文件夹下,或者在C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\dat ...

  5. UML中类结构图示例

  6. C#实现如何判断一个数组中是否有重复的元素

    如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...

  7. nginx 自启动脚本

    nginx 自启动脚本 创建脚本 cd /etc/init.d vi nginx 脚本如下: #! /bin/bash # chkconfig: 35 85 15 # description: Ngi ...

  8. day_4.23 简易计算器

    ''' 简易加减乘除计算器demo 2018-4-23 19:32:49 ''' #1.界面 print("="*50) print(" 欢迎使用计算器v0.1" ...

  9. E - TOYS

    来源 poj 2318 Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad ...

  10. shell参数扩展

    http://zuyunfei.com/2016/03/23/Shell-Truncate-File-Extension/