123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
假设你有一个数组,它的第 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的更多相关文章
- 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...
- [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 ...
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- 通过xmanager连接Linux图形界面
今天要在linux下安装数据库,用的是xmanager:之前自己也用过该工具在OUI下做过安装,还很顺利. 但是,今天连接后,运行命令,等了有5分钟,没有出现OUI界面. linux版本: [root ...
- C语言变量声明问题——变量定义一定要放在所有执行语句/语句块的最前面吗?
报错信息:error C2065: 'salary' : undeclared identifier #include <stdio.h> void main(){ printf(&quo ...
- hadoop Namenode因硬盘写满无法启动
当写元数据的分区写满,可能导致namenode挂掉从而导致及时清理出大块的空间也无法启动namenode,那此时系统namenode会报错 org.apache.hadoop.hdfs.server. ...
- hdu-5675 ztr loves math(数学)
题目链接: ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- codeforces 667D D. World Tour(最短路)
题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- object_funs.py
#__init__ 构造方法,双下划线 #__del__ 析构方法,在对象就要被垃圾回收前调用.但发生调用 #的具体时间是不可知的.所以建议尽量避免使用__del__ print('-------ex ...
- vue之安装配置
直接上图