123. Best Time to Buy and Sell Stock III ——LeetCode
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 two transactions.
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
题目大意:与之前的题目类似,不过这次有次数限制。最多两次买卖,问最大获利。
思路:如果将数组拆成两半,那么每一半都可以用Best Time to Buy and Sell Stock I 题目中的方式获取到最大获利,前后加起来,就是最大获利。用left[i]表示从0到i的最大获利,遍历一遍可求得。用right[i]表示从i到length-1的最大获利,从右向左遍历一遍可获得。从左往右遍历时,一边找最小price一边更新max获利;从右往左遍历时,一边找最大price,一边更新当前max获利。两遍遍历,时间复杂度O(n),讨论区还有一遍遍历的解法,有兴趣可以去看看。
public static int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
int[] left = new int[prices.length];
int[] right = new int[prices.length];
int min = prices[0];
for (int i = 1; i < prices.length; i++) {
left[i] = Math.max(prices[i] - min, left[i - 1]);
min = Math.min(min, prices[i]);
}
int max = prices[prices.length - 1];
int res = 0;
for (int i = prices.length - 2; i >= 0; i--) {
right[i] = Math.max(max - prices[i], right[i + 1]);
max = Math.max(max, prices[i]);
res = Math.max(left[i] + right[i], res);
}
return res;
}
123. Best Time to Buy and Sell Stock III ——LeetCode的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- [leetcode]123. 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 ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 123. 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 a ...
- LeetCode OJ 123. 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 ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- model操作涉及的所有字段(API)
一旦 数据模型 创建完毕, 自然会有存取数据的需要.本文档介绍了由 models 衍生而来的数据库抽象API,及如何创建,得到及更新对象. 贯穿本参考, 我们都会引用下面的民意测验(Poll)应用程序 ...
- Java复习之Eclipse快捷键大全
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- js中判断对象是否存在
s中判断对象是否存在,写法有很多种: 第一种:if (!myObj) { var myObj = { }; }第二种:var global = this; if (!global.myObj) { ...
- c# 文件名排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- Html5的localStorage与sessionStorage五种循序渐进的使用方法
需求:本地记录用户上次输入的内容 使用关键技术:localStorage 第一步:使用jQuery的普通写法 1.JS代码 // 获取window的localStorage对象 var localS ...
- hibernate 性能优化之 1+N 问题
1. 注意 session.clear()的运用,尤其在不断分页查询的时候 a) 在一个大集合中进行遍历,遍历 msg,去除其中的含有敏感字样的对象 b) 另外一种形式的内存泄漏 面试题:Java 有 ...
- Mantis中的状态
在 Mantis中的 问题状态一共有以下几种 10:new,20:feedback,30:acknowledged,40:confirmed,50:assigned,80:resolved,90:cl ...
- 两种计算Java对象大小的方法
之前想研究一下unsafe类,碰巧在网上看到了这篇文章,觉得写得很好,就转载过来.原文出处是: http://blog.csdn.net/iter_zc/article/details/4182271 ...
- 框架页面jquery装载
- IEEP-网络设计
IEEP-网络设计 网络设计概述 网络设计概述 1.负责把网络规划阶段获得的客户需求运用技术手段予以规范化体现 2.网络设计一般遵循模块化指导方针,分模块进行设计 3.网络设计的输出成果必须是规范的. ...