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 ...
随机推荐
- 关于wkwebview
一.引入库 #import <WebKit/WebKit.h> 二.初始化有两种方式 // 默认初始化 - (instancetype)initWithFrame:(CGRect)fram ...
- PHP不使用递归的无限级分类
不用递归实现无限级分类,简单测试了下性能比递归稍好一点点点,但写得太复杂了,还是递归简单方便点 代码: <?php $list = array( array('id'=>1, 'pid'= ...
- linux入侵控制与痕迹清理
后门 (1)开机自动反弹shell (2)linux后门 Rookit 目前常用的有:t0rn /mafix/enyelkm 等 mafix rootkit Mafix是一款常用的轻量应用级别Root ...
- Office下载地址
文件名cn_office_professional_plus_2016_x86_x64_dvd_6969182.isoSHA1277926A41B472EE38CA0B36ED8F2696356DCC ...
- 利用Paramiko模块远程连接Linux
使用Paramiko模块模拟SSH远程连接到服务器,并执行命令.(支持Tab键补全) 1.安装相关模块: 1)安装 Crypto 模块: 下载源码包解压 安装: sudo python setup.p ...
- H5移动端页面设计心得分享(转载)
去年JDC出了不少优秀的武媚娘…不,H5呢,大家都很拼,同时当然也积累了一些经验和教训,今天结合咱们的实战案例,从字体,排版,动效,音效,适配性,想法这几个方面好好聊一聊关于H5的设计,希望对同学们有 ...
- jquery中get传输方法实现读取xml文件
xml文件: <?xml version="1.0" encoding="gb2312"?> <china> <province ...
- put a favicon for github pages
put the picture "favicon.ico" in the root of your web page repo.then add the following lin ...
- 项目有叉号, 但是没有代码错误的时候, 是JDK版本的问题
windows---proferences---java--compiler进入项目--properties---java Compiler进入项目--properties---Myeclipse-- ...
- Framebuffer原理、使用、测试系列文章,非常好的资料,大家一起学习
转载:http://blog.csdn.net/tju355/article/details/6881372 *一.FrameBuffer的原理* FrameBuffer 是出现在 2.2.xx 内核 ...