题目

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.

Solution

  • global(n, k) 表示前n天中一共进行k次交易,所能获取到的最大收益
  • local(n, k) 表示前n中一共进行k次交易,且最后一次卖出发生在第n天,所能获取的最大收益

于是就有递推式子例如以下:

  • global(n, k) = max(local(n, k), global(n-1, k));

    两种情况:

    1,最后一天卖出

    2,最后一天没有卖出

  • local(n, k) = max(local(n-1, k)+diff, global(n-1, k-1), global(n-1,k-1)+diff);

    三种情况:

    1,第n-1天卖出。而且第n-1天买入,然后第n天卖出。那尽管是k+1次交易,可是最后两次交易能够合并成一次,因此也算是k次交易

    2,前n-1天内已经进行了k-1次交易。然后第n天买入,并卖出

    3。前n-1天内进行了k-1次交易,然后第n-1天买入,第n天卖出

$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('

    ').addClass('pre-numbering').hide();
    $(this).addClass('has-numbering').parent().append($numbering);
    for (i = 1; i ').text(i));
    };
    $numbering.fadeIn(1700);
    });
    });

Best time to buy and sell stocks IV的更多相关文章

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

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

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

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

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

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

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

  7. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

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

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

随机推荐

  1. 使用 gulp-file-include 构建前端静态页面

    前言 虽然现在单页面很流行,但是在 PC 端多页面还是常态,所以构建静态页面的工具还有用武之地.最近也看到了一些询问如何 include HTML 文件的问题. 很多时候我们在写静态页面的时候也希望能 ...

  2. JavaScript之数组五大迭代方法总结

    ES5定义了五个迭代方法,每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的),作用域对象将影响this的值.传入这些方法中的函数会接收三个参数:数组的项的值.该项在数组中 ...

  3. mapbox-gl 开发包dev生成

    mapbox-gl简介 mapbox-gl采用webgl,提供在线地图实时渲染功能,具有以下特点: 1.多图层显示 2.图层元素显示样式在颜色.字体.大小范围等.是否显示等可实时更改 3.定位抓取选择 ...

  4. [转载] 2 分钟读懂大数据框架 Hadoop 和 Spark 的异同

    转载自https://www.oschina.net/news/73939/hadoop-spark-%20difference 谈到大数据,相信大家对Hadoop和Apache Spark这两个名字 ...

  5. Vocabulary & Phrase

    Vocabulary A ANSI    美国国家标准学会,American National Standards Institute的缩写 a couple of    [口语]少数的,几个 a s ...

  6. MySQL plugin结构

    1.背景 MySQL插件安装语法如下: 13.7.3.3 INSTALL PLUGIN Syntax INSTALL PLUGIN plugin_name SONAME 'shared_library ...

  7. Python之uuid模块

    UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID —— Universally Unique IDentifier P ...

  8. [转]查询sqlserver 正在执行的sql语句的详细信息

    包含用户名,所在数据库,执行的sql语句,执行开始时间,驱动程序,主机名称 SELECT     [Spid] = session_Id, ecid, [Database] = DB_NAME(sp. ...

  9. flask + Python3 实现的的API自动化测试平台---- IAPTest接口测试平台

    **背景: 1.平时测试接口,总是现写代码,对测试用例的管理,以及测试报告的管理持久化做的不够,              2.工作中移动端开发和后端开发总是不能并行进行,需要一个mock的依赖来让他 ...

  10. LKD: Chapter 9 An Introduction to Kernel Synchronization

    This chapter introduces some conception about kernel synchronization generally. Critical Regions: Co ...