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 (i.e., you must sell the stock before you buy again).

Example 1:

题目

和之前一样,这次你至多能买卖两次。

思路

1. Split the array into two parts, one trade each.

2. Say that f(i) stands for max profit in [0,i] , g(i) stands for max profix in [i, n-1] , then update and finally return max(f(i) + g(i))

代码

 // Best Time to Buy and Sell Stock III
// 时间复杂度O(n),空间复杂度O(n)
public class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 2) return 0; final int n = prices.length;
int[] f = new int[n];
int[] g = new int[n]; for (int i = 1, valley = prices[0]; i < n; ++i) {
valley = Math.min(valley, prices[i]);
f[i] = Math.max(f[i - 1], prices[i] - valley);
} for (int i = n - 2, peak = prices[n - 1]; i >= 0; --i) {
peak = Math.max(peak, prices[i]);
g[i] = Math.max(g[i], peak - prices[i]);
} int max_profit = 0;
for (int i = 0; i < n; ++i)
max_profit = Math.max(max_profit, f[i] + g[i]); return max_profit;
}
}

[leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三的更多相关文章

  1. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

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

  4. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. leetcode 123. Best Time to Buy and Sell Stock III ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. Leetcode#123 Best Time to Buy and Sell Stock III

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

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

  9. 【刷题-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 ...

随机推荐

  1. vue:在路由跳转中使用拦截器

    1:首先在路由对象中的某一个具体的路由对象加这样一个属性 meta: {  requireAuth:true  } 2:然后在main.js中添加这段代码 router.beforeEach((to, ...

  2. C语言复习:内存模型1

    数据类型本质分析 数据类型概念 "类型"是对数据的抽象; 类型相同的数据有相同的表现形式/存储格式以及相关的操作; 程序中使用的所有数据都必定属于某一种数据类型; 数据类型本质思考 ...

  3. 实现一个简单的shell

    使用已学习的各种C函数实现一个简单的交互式Shell,要求:1.给出提示符,让用户输入一行命令,识别程序名和参数并调用适当的exec函数执行程序,待执行完成后再次给出提示符.2.该程序可识别和处理以下 ...

  4. Redis使用认证密码登录

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  5. UploadFtp

    #!/bin/bash FILENAME=$ DSTDIR=$ FTPSRV=ip FTPUSER="user" FTPPWD="password" SRCDI ...

  6. 学JS的心路历程-物件与原型(一)

    前两天说明面向对象的三大特性及JS不符合面向对象,只能称作支持面向对象而已,今天我们来看看JS的原型继承. 首先我们先来看,什么是原型(vmwork): 两个物件之间的原型关系(prototype r ...

  7. node 跨域

    app.post('/api/list',function(req, res){ let reqOrigin = req.headers.origin; // request响应头的origin属性 ...

  8. window上安装 MongoDB 及其 PHP扩展

    window上安装 MongoDB 及其 PHP扩展   工具/原料   window MongoDB MongoDB 方法/步骤     MongoDB 下载 MongoDB提供了可用于32位和64 ...

  9. Navicat远程连接不上mysql解决方案(已测试过)

    内容参考网上的文章,此处只做记录. 一.can‘t connect to MySql server on ‘192.168.X.X’ 这是因为mysql端口被防火墙拦截,需用linux执行如下指令:( ...

  10. opencv批量读取图片

    #include<opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    int num=4;// ...