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]的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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. ...

  9. 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 ...

  10. 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题使用动态 ...

随机推荐

  1. XtraBackup物理备份 阿里云的Mysql备份方案

    XtraBackup物理备份 Percona XtraBackup是世界上唯一的开源,免费的MySQL热备份软件,为InnoDB和XtraDB 数据库执行非阻塞备份.使用Percona XtraBac ...

  2. Hibernate 案例

    搭建一个Hibernate环境,开发步骤: 1. 下载源码 版本:hibernate-distribution-3.6.0.Final 2. 引入jar文件          hibernate3.j ...

  3. csvn install guide

    一. make sure java install $ java -version $ echo $JAVA_HOME 二. untar tgz file $ tar xf CollabNetSubv ...

  4. MongoDB--操作符

    $gt -- > $lt -- < $gte -- >= $lte -- <= $all 与 in 类似,不同的是必须满足[]内所有的值 $exists 字段是否存在 db.s ...

  5. crontab问题处理

    用pyhton写了一些爬虫,由于数据量比较大,需要跑的时间也比较长,所以将代码部署到服务器上.选择用crontab完成爬虫的定时爬取数据,这样避免了人工的干预,减少一些人为错误.但在部署crontab ...

  6. 如何离线安装Visual Studio 2017

    1. 官方下载在线安装文件 vs_community.exe https://www.visualstudio.com/zh-hans/thank-you-downloading-visual-stu ...

  7. C语言精要总结-指针系列(二)

    此文为指针系列第二篇: C语言精要总结-指针系列(一) C语言精要总结-指针系列(二) 指针运算 前面提到过指针的解引用运算,除此之外,指针还能进行部分算数运算.关系运算 指针能进行的有意义的算术运算 ...

  8. 【Android Developers Training】 38. 文件共享需求

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 自己开源的leaf-snowflake

    拜读了美团点评技术团队博客的"Leaf--美团点评分布式ID生成系统(http://tech.meituan.com/MT_Leaf.html)"之后,收获很多.纸上得来终觉浅 绝 ...

  10. flask 上传头像

    上传头像,自己感觉了好久,就是上传文件呗其实,存在一个路径,数据库存储这个路径,然后展示给前端,啥都不说,看怎么实现的. 数据库设置如下 user_image=db.Column(db.String( ...