[leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

时间:2022-04-21 13:16:27

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. &lbrack;leetcode&rsqb;122&period; 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 &colon; 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. &lbrack;LeetCode&rsqb; 123&period; 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&period; 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&period; Best Time to Buy and Sell Stock III &lpar;stock problem&rpar;

    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&num;123 Best Time to Buy and Sell Stock III

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

  8. 【leetcode】123&period; 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. SSH ProxyCommand

    简单的说就是SSH层面的代理服务器,实现通过跳板机执行SSH相关命令到目标机器的代理服务.

  2. C&plus;&plus; 虚函数畅谈

    0x01:前言 虚函数是C++里最重要的概念之一,并且是判定C++是否入门的一个热门问题.今天这篇文章简单谈谈虚函数. 0x02:虚函数简介 虚函数可以被子类实现函数所覆盖. virtual是关键字, ...

  3. 如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入

    大多数脚本利用发生在用户可以将可执行代码(或脚本)插入您的应用程序时. 默认情况下,ASP.NET 提供请求验证.只要窗体发送包含任何 HTML,该验证都会引发错误. 您可以使用下列方法防止脚本利用: ...

  4. 配置普通用户可以运行saltstack的模块

    client_acl 配置使用 1.概述:开启对系统上非root的系统用户在master上可以执行特殊的模块.这些模块名可以使用正则表达式来表示,不能指定对哪些minion执行命令.执行命令只需要切换 ...

  5. patchdiff2 函数比较插件

    https://code.google.com/archive/p/patchdiff2/downloads

  6. Sql语句中IN等方面的用法

    select * from txt1 select * from txt2 select * from txt1 where name in (select name from txt2 where ...

  7. DevExpress ASP&period;NET 使用经验谈(9)-Dev控件客户端事件 ClientSideEvents

    上一节,已经介绍了ASPxGridView的自定义列和基本事件 ,本节接着将介绍Dev控件的客户端事件模型. 在上节示例基础上,我们增加一行菜单,使用Dev的ASPxMenu来实现,如下图所示. 图一 ...

  8. &lowbar;vimrc 的配置

    windows set nocompatible set guifont=Consolas:h17 color molokai set backspace=2 set sts=4 set ts=4 s ...

  9. ZEOSDBO-7安装

    zeosdbo是一套免费开源的Delphi数据库连接组件,可连接mssql.mysql.sybase.oracle.firebird.sqlite.postgresql等多种数据库.调用方法简单. 连 ...

  10. 显示器如何显示一个YUV422格式的图形

    记录在开发过程中对知识点的一些理解: 在开发渲染程序的过程中,需要对视屏文件进行解码解码后特效文件的叠加,使用的技术是(FFmpeg+DirectX) 解码出来的视屏数据格式是YUYV,使用Direc ...