LeetCode-Best Time to Buy and Sell Stock III[dp]
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).
标签: Array Dynamic Programming
分析:动态规划,设left[i]表示0-i天的最大利润,right[j]表示j-n-1天的最大利润,所以状态方程为:
left[i]=max(left[i-1],prices[i]-minleft); minleft表示0-i天的最低价
right[j]=max(right[j+1],maxright-prices[j]); maxright表示j-n-1天的最高价;
由于只可以买卖两次,并且在第二次买进是必须把第一次的卖掉,所以最大利润为max(left[i]+right[i]);
参考代码:
public class Solution {
public int maxProfit(int[] prices) {
int len=prices.length;
if(len<2)
return 0;
int left[]=new int[len];
int right[]=new int[len];
int minleft=prices[0];
left[0]=0;
for(int i=1;i<len;i++){
minleft=Math.min(minleft, prices[i]);
left[i]=Math.max(left[i-1], prices[i]-minleft);
}
int maxright=prices[len-1];
right[len-1]=0;
for(int j=len-2;j>=0;j--){
maxright=Math.max(maxright, prices[j]);
right[j]=Math.max(right[j+1], maxright-prices[j]);
}
int maxProfit=left[0]+right[0];
for(int i=0;i<len;i++){
maxProfit=Math.max(maxProfit, left[i]+right[i]);
}
return maxProfit;
}
}
LeetCode-Best Time to Buy and Sell Stock III[dp]的更多相关文章
- 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
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- 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题使用动态 ...
随机推荐
- 关于oracle数据库备份还原-impdp,expdp
初始化: -- 创建表空间 CREATE TABLESPACE 表空间名 DATAFILE '文件名.dat' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNL ...
- Spring学习(2)---IOC
1.接口及面向接口编程 2.什么是IOC 3.Spring的Bean配置 4.Bean的初始化 5.Spring的常用注入方式 (一)接口 用于沟通的中介物的抽象化 实体把自己提供给我外接的一种抽象化 ...
- Java中SimpleDateFormat用法详解
所有已实现的接口: Serializable, Cloneable SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本) ...
- String 类问题发现与解决
1.在代码中出现:String t = null; t.length(); 执行后:控制台报:java.lang.NullPointerException 原因:Java中,null是一个关键字,用来 ...
- 宠物收养场 Treap
宠物收养场 时间限制: 1 Sec 内存限制: 128 MB 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠 ...
- Lamp单独安装(windows下)
安装的软件清单:apache_2.2.9-win32-x86-openssl-0.9.8h-r2.msimysql-5.1.28-rc-win32.zipphp-5.2.6-Win32.zipphpM ...
- 获取元素到body/html的距离函数
获取元素到body的距离: <script> function offsetDis(obj) { var l = 0, t = 0; while(obj) { l = l + obj.of ...
- 插入多行数据的时候,一个insert插入多行
如:insert into t_users(a,b,c)value('1','2','3'),('3','4','5'),('6','7','8') ('1','2','3'),('3','4','5 ...
- 配置SSH无秘钥登录
[hadoop@hadoop01 ~]$ cd .ssh [hadoop@hadoop01 .ssh]$ ls authorized_keys id_rsa id_rsa.pub known_host ...
- Bootstrap提示信息(标签、徽章、巨幕和页头)
前面的话 在Bootstrap中,有一些组件用于提示信息,如 标签.徽章.巨幕和页头.本文将详细介绍Bootstrap提示信息 标签 在一些Web页面中常常会添加一个标签用来告诉用户一些额外的信息,比 ...