Lintcode150 Best Time to Buy and Sell Stock II solution 题解
来源:互联网 发布:mamp mysql 启动失败 编辑:程序博客网 时间:2024/05/19 05:40
【题目描述】
Say you have an array for which theith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。
【题目链接】
www.lintcode.com/en/problem/best-time-to-buy-and-sell-stock-ii/
【题目解析】
卖股票系列之二,允许进行多次交易,但是不允许同时进行多笔交易。直觉上我们可以找到连续的多对波谷波峰,在波谷买入,波峰卖出,稳赚不赔~。那么这样是否比只在一个差值最大的波谷波峰处交易赚的多呢?即比上题的方案赚的多。简单的证明可先假设存在一单调上升区间,若人为改变单调区间使得区间内存在不少于一对波谷波峰,那么可以得到进行两次交易的差值之和比单次交易大,证毕。
计算所有连续波谷波峰的差值之和。需要遍历求得所有波谷波峰的值吗?其实可以把数组看成时间序列,只需要计算相邻序列的差值即可,只累加大于0的差值。
【参考答案】
www.jiuzhang.com/solutions/best-time-to-buy-and-sell-stock-ii/
- Lintcode150 Best Time to Buy and Sell Stock II solution 题解
- Lintcode149 Best Time to Buy and Sell Stock solution 题解
- [LeetCode] Best Time to Buy and Sell Stock II Solution
- LeetCode题解-Best Time to Buy and Sell Stock II
- LeetCode题解:Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II题解
- LeetCode 121. Best Time to Buy and Sell Stock & 122. Best Time to Buy and Sell Stock II题解
- [LeetCode] Best Time to Buy and Sell Stock Solution
- [LeetCode] Best Time to Buy and Sell Stock III Solution
- Lintcode151 Best Time to Buy and Sell Stock III solution 题解
- LeetCode - Best Time to Buy and Sell Stock III 题解
- LeetCode题解-Best Time to Buy and Sell Stock
- LeetCode题解-Best Time to Buy and Sell Stock III
- LeetCode题解:Best Time to Buy and Sell Stock
- LeetCode题解:121. Best Time to Buy and Sell Stock
- Leetcode题解 121. Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock题解
- LeetCode121. Best Time to Buy and Sell Stock题解
- Hibernate中一对一映射详细解析(内含实例)
- C++ Thread
- 【POJ 3667】 hotel 【线段树 +区间操作+区间合并】
- 第一章-问答题
- 回溯——哈密顿回路问题
- Lintcode150 Best Time to Buy and Sell Stock II solution 题解
- 从CNN视角看在自然语言处理上的应用 原创 2017年10月24日 00:00:00 1339 作者 | 卞书青 卷积神经网络(Convolutional Neural Network)最早是应用在
- 今天几乎没有收获
- 堆和栈的区别
- Python 3.6 爬虫爬取豆瓣《孤芳不自赏》短评
- easyui
- Python 编程笔记(二)
- CentOS6.4下Mysql数据库的安装与配置
- 2017年含金量最高的机器学习技能或知识有哪些? 翻译 2017年10月20日 14:22:44 标签: 机器学习 / quora 7504 原文:As of 2017, what set of