Best time to buy and sell stocks IV
题目
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 = $('
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i ').text(i));
};
$numbering.fadeIn(1700);
});
});
Best time to buy and sell stocks IV的更多相关文章
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【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 ...
- 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. ...
- 【刷题-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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- SQL系统函数的使用(实验五)
SQL系统函数的使用(试验5) 函数在查询语句中的使用 查询员工的姓名和日工资(保留1位小数): 查询并显示部门号为01和02的所有员工的姓名首字及岗位: 查询并显示所有员工的姓名及工龄: 查询199 ...
- WebSocket小插件
一.WebSocket小介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信 ...
- 最大流——Dinic算法
前面花了很长时间弄明白了压入-重标记的各种方法,结果号称是O(V3)的算法测demo的时候居然TLE了一个点,看了题解发现所有人都是用Dinic算法写的,但它的复杂度O(V2E)明显高于前者,具体是怎 ...
- EF分页
先来看看几个LINQ to SQL的几个函数. Take 说明:获取集合的前n个元素:延迟.即只返回限定数量的结果集. var q = ( from e in db.Employees order ...
- 分享:苹果APP更新上架被拒的另一种理由(Safety - Objectionable Content)
这两个星期,本来想和大伙分享:写IT连创业系列运营篇. 但时间飞过,仍只是写了开头,一直很忙,没能完往下写. 今天就动手写点其它内容,哈哈,免的和小伙伴太陌生〜〜〜 前几天更新了:IT恋和IT连的版本 ...
- 通过游戏认识 --- JQuery与原生JS的差异
前言 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库( 或JavaScript框架).jQuery设计的宗旨是“write ...
- Storm入门之第一章
Storm入门之第一章 1.名词 spout龙卷,读取原始数据为bolt提供数据 bolt雷电,从spout或者其他的bolt接收数据,并处理数据,处理结果可作为其他bolt的数据源或最终结果 nim ...
- Java父线程(或是主线程)等待所有子线程退出
static void testLock1(){ final AtomicInteger waitCount = new AtomicInteger(30000); final Object wait ...
- MatlabR2015b用了一段时间之后需要重新激活
问题描述:MatlabR2015b安装成功,用了一段时间之后需要重新激活,按照http://www.cr173.com/soft/490645.html教程,无法激活成功,卸载重新安装也不行. 解决方 ...
- 4. 跟踪标记 (Trace Flag) 610 对索引组织表(IOT)最小化日志
跟踪标记:610 功能: 用批量导入操作(Bulk Import Operations)加载数据时,对于索引组织表(即有聚集索引的表) 最小化日志: 上图为simple/bulk-logged恢复模式 ...