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 ...
随机推荐
- 怎么理解impala(impala工作原理是什么)
下面给大家介绍怎么理解impala,impala工作原理是什么. Impala是hadoop上交互式MPP SQL引擎, 也是目前性能最好的开源SQL-on-hadoop方案. 如下图所示, impa ...
- MYSQL系列-MYSQL基础增强(Myql函数)
MYSQL基础增强(Myql函数) 在这里只介绍一些常用的,比较新颖的: 字符串函数: CONCAT://字符串连接函数 mysql> SELECT CONCAT('My', 'S', 'QL' ...
- vsftpd配置文件解析
对vsftpd配置文件详细解答. 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为f ...
- How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover
http://java.dzone.com/articles/how-configure-tomcatjboss-and In this post we will see how to setup a ...
- jquery点击事件后增加克隆的标签,并改变克隆的属性加入
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《Unity系列》Json文件格式的解析——初级教程
Json作为轻量级的数据交换格式,被广泛应用于网络数据传输中.相关的解析工具层出不穷,一般掌握一个工具的应用其他的相应工具就能立马学会. 这里以C#中的LitJson为例给大家示范一下解析工具的用法. ...
- HashMap和Hashtable存放null
Hashmap是可以放key为null的,Hashtable不能放key为null.hashtable放key为null会报空指针异常 1. hashmap put方法源码 public V put( ...
- Linux 套接字编程 - select
select 可以感知文件表述符集合中的变化,如果办fd0(即标准输入)放入select的read fd set,发现只有按回车的时候select才会返回.查了下要把终端的缓冲大小设为1,这样就能实现 ...
- WinSock 重叠IO模型
title: WinSock 重叠IO模型 tags: [WinSock 模型, 网络编程, 重叠IO模型] date: 2018-06-29 20:26:13 categories: Windows ...
- iOS中使用RNCryptor对资源文件加密
原文:http://blog.csdn.net/chenpolu/article/details/46277587 RNCryptor源码https://github.com/RNCryptor/RN ...