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. Python的核心数据结构

    数据结构 例子 数字 1234,3.1415,3+4j 字符串 'spam'."grace's" 列表 [1,[2,'three'],4] 字典 {'food':'spam','t ...

  2. AES加密解密算法---java

    package com.BFGJ.AES; import java.util.Random; import java.util.StringTokenizer; import javax.crypto ...

  3. solr学习笔记section2-solr单机(节点)简单的core操作

    在上一节中我们已经成功部署和运行了一个solr应用,那么我们就可以通过这个正在运行的solr来创建一些文档,并进行搜索. 首先介绍一下core这个概念,core在solr中类似与关系型数据库中一张表的 ...

  4. 允许mysql用户从远程登录

    1.修改/etc/mysql/my.cnf,将下面的行注释掉bind=127.0.0.1注释#bind=127.0.0.1 2.修改用户权限,允许从任何主机登录mysql>use mysql;m ...

  5. python 标准库 -- unittest

    一. unittest 单元测试 编写单元测试 示例代码 : import unittest from flask import current_app from app import create_ ...

  6. docker 内部组件结构 -- docker daemon, container,runC

    Docker, Containerd, RunC : 从 Docker 1.11 开始, docker 容器运行已经不是简单地通过 Docker Daemon 来启动, 而是集成了Container, ...

  7. 移动webAPP前端开发技巧汇总

    1. viewport:webapp视图 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是除去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动 ...

  8. An abandoned sentiment from past

    An abandoned sentiment from past time limit per test 1 second memory limit per test 256 megabytes in ...

  9. 微信jssdk分享功能,jssdk成功调用,分享内容自定义失败

    前提:调用微信jssdk分享功能,通过微信开发者工具调试,调用正常,无任何报错信息. 问题:调用成功,且开发者工具正常显示,但是通过真机调试,分享出去后,自定义内容失效,为微信自动获取的默认内容!截止 ...

  10. 如何在前端模版引擎开发中避免使用eval函数

    前段时间,想着自己写一个简单的模版引擎,便于自己平时开发demo时使用.于是根据自己对模版引擎的理解,定义自己的模版格式,然后,根据自己定义的格式,编写处理函数,将模版标签中的字符串,解析成可执行的字 ...