假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来找到最大的利润。你最多可以完成两笔交易。
注意:
你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
详见:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/

Java实现:

class Solution {
public int maxProfit(int[] prices) {
int n=prices.length;
if(prices == null || n <= 1){
return 0;
}
int maxProfit = 0; int min = prices[0];
int forward[] = new int[n];
for(int i=1;i<n;i++){
min=Math.min(min,prices[i]);
forward[i]=Math.max(forward[i-1],prices[i]-min);
} int max = prices[n-1];
int back[] = new int[n];
for(int i = n-2; i >= 0; i--){
max = Math.max(prices[i],max);
back[i] = Math.max(max-prices[i],back[i+1]);
} for(int i = 0; i < n; i++){
maxProfit = Math.max(maxProfit,forward[i] + back[i]);
} return maxProfit;
}
}

参考:https://www.cnblogs.com/springfor/p/3877068.html

123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III的更多相关文章

  1. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...

  2. [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机

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

  3. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

  4. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    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] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    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 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)

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

  8. 3.Best Time to Buy and Sell Stock(买卖股票)

    Level: ​ ​ Easy 题目描述: Say you have an array for which the ith element is the price of a given stock ...

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

随机推荐

  1. DTD复习笔记(复习资料为菜鸟教程里的DTD教程)

    DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 为什么使用 DTD? 通过 DTD,您的每一个 XML 文件均可携带 ...

  2. 【bzoj3210】花神的浇花集会

    将(x,y)转化成(x+y,x-y)可以将切比雪夫距离转化成曼哈顿距离(自己推一推) A.B的切比雪夫距离就是A‘.B‘曼哈顿距离的一半. 那么可以将x.y分离处理,排序中位数即可. 注意如果最后选的 ...

  3. Principle of least astonishment

    Principle of least astonishment - Wikipedia https://en.wikipedia.org/wiki/Principle_of_least_astonis ...

  4. js对table操作(添加删除交换上下TR)

    <table width="100%" border="0" cellpadding="2" cellspacing="1& ...

  5. Fibonacci数列(找规律)

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  6. HDU3746 Cyclic Nacklace —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. 异常、Throwable、finally、File类(十九)

    1.异常的概述和分类 * A:异常的概述 * 异常就是Java程序在运行过程中出现的错误.* B:异常的分类 * 通过API查看Throwable * Error * 服务器宕机,数据库崩溃等 * E ...

  8. 重入锁ReentrantLock用法以及如何实现重进入

    在java多线程中,可以使用synchronized实现线程之间的同步互斥,但在jdk1.5中增加了ReentrantLock类也能达到同样的效果,而且在使用上更加灵活,扩展功能上更加强大. 创建My ...

  9. UISwitch用法:

    代码: #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  10. Vim Vundle YouCompleteMe

    /************************************************************************************** * Vim Vundle ...