LeetCode——Best Time to Buy and Sell Stock III
Description:
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天)的最大收益,这样就变成了求一次交易的最大收益了,同系列问题I,
最后遍历一次求子问题最大收益之和的最大值,就是最后的解。时间复杂度O(n),空间复杂度O(n)
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if(len == 0) return 0;
int[] leftProfit = new int[len];
int[] rightProfit = new int[len];
Arrays.fill(leftProfit, 0);
Arrays.fill(rightProfit, 0);
//计算左边的最大收益,从前向后找
int lowestPrice = prices[0];
for(int i=1; i<len; i++) {
lowestPrice = min(lowestPrice, prices[i]);
leftProfit[i] = max(leftProfit[i-1], prices[i] - lowestPrice);
}
//计算右边的最大收益,从后往前找
int highestPrice = prices[len-1];
for(int i=len-2; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
rightProfit[i] = max(rightProfit[i+1], highestPrice-prices[i]);
}
int maxProfit = leftProfit[0] + rightProfit[0];
//遍历找出经过最多两次交易后的最大了收益
for(int i=1; i<len; i++) {
maxProfit = max(maxProfit, leftProfit[i] + rightProfit[i]);
}
return maxProfit;
}
public int min(int a, int b) {
return a > b ? b : a;
}
public int max(int a, int b) {
return a > b ? a : b;
}
}
LeetCode——Best Time to Buy and Sell Stock III的更多相关文章
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- [LeetCode] 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] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- LeetCode: Best Time to Buy and Sell Stock III [123]
[称号] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- [leetcode]Best Time to Buy and Sell Stock III @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...
随机推荐
- Liunx 安装 Mysql 5.7
#[安装 Mysql 5.7] # 00.系统目录说明# 安装文件下载目录:/data/software# Mysql目录安装位置:/usr/local/mysql# 数据库保存位置:/data/my ...
- swconfig--交换接口配置命令
swconfig是交换接口(switch)配置命令. swconfig list ; 列出可用的SWITCH设备信息(dev参数) Found: switch0 - ag71xx-mdio. swco ...
- CentOS6.5下安装Cloudstack
个人记录: 使用yum源安装,地址:http://mirrors.163.com/.help/CentOS6-Base-163.repo 后续待进行
- CentOS安装ssh服务
yum search ssh yum install openssh-server service sshd status [编辑]艺搜参考 http://www.cnblogs.com/eastso ...
- 关于Cocos2d-x中背景音乐和音效的添加
1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...
- nodejs基础 -- 回调函数
Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都 ...
- 安装 oracle [转]
先下载3个东西:链接忘记了,大家自己找一下 1 ORA+11+G+R2+server+64bit+for+windows.iso (Oracle 安装文件) 2 PLSql 3 oracle6 ...
- 对sssp项目搭建的补充,总错误处理。
总错误处理,是为了在程序运行时代码出错能及时在控制台看出错误信息. 1. springMVC配置文件中: -------- 2.controller包中: 新建类FrameControllerAdvi ...
- this总结
this总结,mark一下: Object中的this: Object方法中的this,指向的就是该对象,即谁调用this就指向谁,与C#等服务器语言的思想比较一致. let demo = { nam ...
- js定义对象
1.工厂模式 function createPerson(name,age,job){ var o = {}; o.name = name; o.age = age; o.job = job; o.s ...