原题链接在这里:https://leetcode.com/problems/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 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很大已经超过了prices.length的时候, 若是按照DP的方法做会浪费时间以及空间. 此时可以直接参照Best Time to Buy and Sell Stock II做法.

Let local[i][j] denotes local maximum profit of up when prices up to i, transaction up to j, the last transaction happend at i.

loacal[i][j] = Math.max(global[i-1][j-1] + Math.max(diff, 0), local[i-1][j] + diff).

global[i-1][j-1] is global maximum profit up to prices i-1, transaction up to j-1. The last transaction happen at prices[i]. If could be diff or 0.

local[i-1][j] is local maximum profit up to pricesi-1, transaction up to j. It already perform j transaction, then the stock sold at prices[i-1] must be sold at prices[i].

Time Complexity: O(prices.length * k), k是最多交易次数.

Space: O(k).

AC Java:

 public class Solution {
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0 || k == 0){
return 0;
}
if(k>=prices.length/2){
int res = 0;
for(int i = 1; i < prices.length; i++){
res += Math.max(0, prices[i]-prices[i-1]);
}
return res;
} int[] local = new int[k+1];
int[] global = new int[k+1];
for(int i = 1; i<prices.length; i++){
int diff = prices[i] - prices[i-1];
for(int j = k; j>=1; j--){
local[j] = Math.max(global[j-1] + Math.max(diff,0), local[j] + diff);
global[j] = Math.max(global[j], local[j]);
}
}
return global[k];
}
}

Best Time to Buy and Sell Stock III的general情况.

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

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

  2. LeetCode——Best Time to Buy and Sell Stock IV

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

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

    https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...

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

  5. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

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

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

  8. [LeetCode] 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 ...

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

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

随机推荐

  1. httpclient爬取性感美图

    依赖httpclient4.2,Jsop SemeiziCrawler.java package kidbei.learn.crawler; import java.io.File; import j ...

  2. mysql root密码破解

    破解root密码:方法一:1./etc/my.cnf在[mysqld]段中加入skip-grant-table2.重启mysql3.直接mysql登录3.通过修改权限表方式修改mysql密码(upda ...

  3. hdu Robberies

    这道题目应该在理解上会有一点问题.这道题的概率不是用来加的,而是用来乘的.这道题要的是在能逃跑的前提下,获得的最大money,而题目中给的概率是被抓的概率,所以要先有一个预处理,之后只要列出状态转移方 ...

  4. Scrum会议4

    组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.19 已完成内容: 1.连连看生成一关功能. 2.目前测试发现没有问题. 计划完成: 1. ...

  5. HTTP POST, PUT PATCH

    POST = 新增 GET = 讀取 PUT = 更新 DELETE = 刪除 PUT 会在地址栏显示参数信息,不安全! 理解POST和PUT的区别,顺便提下RESTfu 这两个方法咋一看都可以更新资 ...

  6. Irrlicht引擎I 配置

    游戏是一个比较大的系统,包含了图形引擎.网络.AI.声音.UI等模块,模块的开发可能会分别进行或者采用开源项目,Irrlicht引擎基本包含了这些模块,不过在使用中也会陆续加入其它的模块.以前开发的程 ...

  7. Delphi 记录类型- 结构指针

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. spark mllib 之线性回归

    public static void main(String[] args) { SparkConf sparkConf = new SparkConf() .setAppName("Reg ...

  9. Day09

    Servlet概述 生命周期方法: void init(ServletConfig):出生之后(1次): void service(ServletRequest request, ServletRes ...

  10. 蓝牙4.0的LM层说明

    1.概念 The Link Manager Protocol (LMP) is used to control and negotiate all aspects of the operation o ...