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 ...
随机推荐
- asp.net——初识多线程
1.首先讲解一下什么是线程(该定义是参考线程的百度百科) 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针( ...
- GOPS2017全球运维大会 • 深圳站 历届金牌讲师精选亮相
GOPS2017全球运维大会 • 深圳站将于2017年4月21日-22日在深圳举行,GOPS2017报名平台:活动家! 快捷报名通道:http://www.huodongjia.com/event-2 ...
- spring-事务实现原理
spring事务的实现原理:aop. aop的两种实现方式: 1.动态代理: 事务方法与调用方法不能在同一个类中,否则事务不生效.解决方案,自己注入自己(实质注入的是代理类). 实现方式:jdk动态代 ...
- UNIX基础--磁盘组织
磁盘组织 FreeBSD 查找文件的最小单位是文件名. 而文件名区分大小写,不凭文件扩展名去识别这个文件是 程序. 文档, 或是其他格式的数据. 在文件系统里目录和文件的作用是存储数据. 每一个文件系 ...
- [其他]win7下chrome浏览器插件导出与导入
下载了某些插件,重装电脑怎么不备份,重装之后怎么再次使用,一文搞定! 导出crx格式备份文件 1.选择 自定义格式及控制 > 更多工具 > 扩展程序: 2.勾选"开发者模式&qu ...
- 微信内置浏览器私有接口WeixinJSBridge介绍(转)
这篇文章主要介绍了微信内置浏览器私有接口WeixinJSBridge介绍,本文讲解了发送给好友.分享函数.隐藏工具栏.隐藏三个点按钮等功能,需要的朋友可以参考下 微信网页进入,右上角有三个小点,没错, ...
- 向多个会话窗口发送命令 -SecureCRT
1.前提 一个服务可能部署在多台机器上,这时如果要查问题,最繁复的方法就是打开该服务的每个session,把命令在每一台机器上复制一下执行,找到相关的日志:还有一种方法就是一条命令同时向多个会话窗口发 ...
- drupal7 修改文件上传大小限制
参考文章:Drupal 7 设置上传文件的限制大小 自己用 '#type' => 'managed_file'做了一个上传的功能,但是上传时总是说超过了2M的限制,接下来说一下怎么修改限制. 一 ...
- Mybatis的传参
最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发 最终要的一点事,自己写的话,记忆更加深刻: 首先自己先写了个静态块,防止代码冗余: private static Sq ...
- snmp协议接口
所有网络设备上都会支持smap,获取服务器的基本信息,这样就不用在客户端上装应用就可以检测到基本的信息,是基于socket开发 内存调用这些命令来提取服务器的信息 snmpgetlocalhost - ...