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 ...
随机推荐
- HTML DOM应用案例2
<html> <head> <title>day03</title> <script type="text/javascript&quo ...
- salesforce零基础学习(八十二)审批邮件获取最终审批人和审批意见
项目中,审批操作无处不在.配置审批流时,我们有时候会用到queue,related user设置当前步骤的审批人,审批人可以一个或者多个.当审批人有多个时,邮件中获取当前记录的审批人和审批意见就不能随 ...
- #UnityTips# 2017.11.14
hi,all.最近比较忙,所以更新也比较慢了. 今天就来和大家分享一个小Tip,它是关于UGUI的坑的. 使用过UGUI的朋友们都知道,Canvas的渲染方式有三种: Screen Space Ove ...
- ajax事件请求
首先,ajax是什么? ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. ajax是一种用于创建的快速动态网页的技术. 当async:true时,表示异步执行ajax代码:当as ...
- [转]ORACLE递归查询
转自:http://www.oracle.com/technetwork/cn/articles/hartley-recursive-086819-zhs.html 递归数据库处理,也称为材料清单 或 ...
- iOS推送,看这篇足够了
一.注册推送: - (void)registerNotification { if ([[[UIDevice currentDevice] systemVersion] floatValue] > ...
- C# 排序技术研究与对比
一. 排序场景 一维数组排序 多维数组排序 集合排序,例如Dictionary ,List<T>, 自定义类集合排序等 DataTable排序 二. 排序实现和测试 1. 一维数组排序 1 ...
- Office隐藏17年的漏洞CVE_2017_11882测试记录
Office隐藏17年的漏洞CVE_2017_11882测试记录 创建时间: 2017/11/25 0:18 作者: CN_Simo 标签: Office漏洞 参考文章1:https://www.cn ...
- JavaWeb 学习之 JSTL
上一篇博文我们讲解了 MVC 小案例,案例中包含了基本的增.删.改.查,对这个案例的有兴趣的伙伴可以自己动手实践一下,去复习一下或者说是学点新的知识!如果有已经看过且实践过的伙伴相信对 JSP 页面中 ...
- 项目实战9—企业级分布式存储应用与实战MogileFS、FastDFS
企业级分布式存储应用与实战-mogilefs 环境:公司已经有了大量沉淀用户,为了让这些沉淀用户长期使用公司平台,公司决定增加用户粘性,逐步发展基于社交属性的多样化业务模式,决定开展用户讨论区.卖家秀 ...