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 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 (ie, you must sell the stock before you buy again).
【题目分析】
用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。最多交易两次,手上最多只能持有一支股票,求最大收益。
【思路】
动态规划法。以第i天为分界线,计算第i天之前进行一次交易的最大收益preProfit[i],和第i天之后进行一次交易的最大收益postProfit[i],最后遍历一遍,max{preProfit[i] + postProfit[i]} (0≤i≤n-1)就是最大收益。第i天之前和第i天之后进行一次的最大收益求法同Best Time to Buy and Sell Stock I。
【java代码】
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length < 2) return 0; int n = prices.length;
int preProfit[] = new int[n];
int postProfit[] = new int[n]; int curMin = prices[0];
for(int i = 1; i < n; i++){
curMin = Math.min(curMin, prices[i]);
preProfit[i] = Math.max(preProfit[i-1], prices[i] - curMin);
} int curMax = prices[n-1];
for(int i = n-2; i >= 0; i--){
curMax = Math.max(curMax, prices[i]);
postProfit[i] = Math.max(postProfit[i+1], curMax - prices[i]);
} int maxProfit = 0;
for (int i = 0; i < n; i++) {
maxProfit = Math.max(maxProfit, preProfit[i] + postProfit[i]);
} return maxProfit;
}
}
LeetCode OJ 123. Best Time to Buy and Sell Stock III的更多相关文章
- 【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
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- [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 笔记23 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 ...
- 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 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- unity3d打开对话框
最近一直在忙项目,没时间更新博客,这两天趁空封装windows下的打开对话框,支持多选.其他系统可以用ngui或者ugui封装一个. 这里就不上封装的源码了提供dll供小伙伴们使用,如果有需要源码请请 ...
- 视频编辑类sdk--lansoeditor--更新啦, 完全免费,欢迎下载
当前版本是20160506 beta, 增加了兼容高通的64位硬件编码器增加MediaInfo类,您可以轻松获取多媒体中的各种信息.增加了演示的15个ffmpeg处理方法,您可以用这些方法实现秒拍图像 ...
- 制作自己的web字体
今天教给大家制作自己的web字体
- AndroidStudio0.5.2 BUG 导致 menu 菜单键崩溃
郁闷了半天,今天发现一点击手机 menu 键应用就崩溃了,记得之前都是好好的,调试了半天代码还是搞不定,于是网上google了一番,发现仅国外有一两篇文章有提到类似问题,据说是 0.5.2 版本的 B ...
- myeclipse启动服务器时,tomcat出错问题
启动服务器,错误提示: Launching web on myeclipse tomcat has encountered a problem An internal error occurred d ...
- 洛谷-拼数-NOIP1998提高组复赛
题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4 ...
- linux提示语言包
locale -a 查看可用的语言包.选择中文语言包 echo 'LC_ALL="zh_CN.utf8"' >> /etc/profileecho 'export LC ...
- Spring MVC(一)
MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域,MVC特指一种表现层设计模式,不限于Java语言 spring mvc属于spring框架的后续产品,用在基于MVC的表现层开发,类 ...
- OvS: data structure analysis
hmap usage: in include/openvswitch/shash.h, we have: at first glance, it is a hmap encapsulated in s ...
- 《JavaScript高级程序设计》读书笔记 ---理解对象
上一章曾经介绍过,创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示.var person = new Object();person.name = &qu ...