[leetcode]123. 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 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 最佳炒股时机之三的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode#123 Best Time to Buy and Sell Stock III
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- 【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 ...
- 【刷题-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.加快Xshell客户端连接到CentOS的速度
1.编辑打开ssh的配置文件 /etc/ssh/sshd_config 找到里面的UseDNS yes修改为:#UseDNS no service sshd restart
- DbUtil数据库连接
DbUtil数据库连接 package com.zjx.util; import java.sql.Connection; import java.sql.DriverManager; public ...
- 记录Git的安装过程
从https://git-scm.com/download/win,选择Windos版本下载. 选择打开的工具,用的Notepad. 下一步 下一步
- 434. Number of Segments in a String
原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...
- Java 分页与原理(上)
Java web 实习需要用到分页技术 所以现在学习一下 做个记录 方便以后查阅 分类:传统分页技术 下拉式分页技术 起始位置(0)开始 查询(10条记录)
- winform下利用webBrowser执行javascript
目前很多网站为了防止恶意提交表单信息,大多都采用了加密的方式对提交信息进行处理,加密处理后通过POST提交给服务器验证,这种操作一般都是用Javascipt进行加密,若是我们想要正确提交表单到网站,就 ...
- C#关键字as出现的错误
ObjectCache cache = MemoryCache.Default; string cacheData1 = cache["key1"] as string;//得不到 ...
- python基础学习Day14 内置函数 匿名函数
一.内置函数里几个高频重要函数 (1)min\max函数的用法 以min函数的为例: min:返回可迭代对象的最小值(可加key,key为函数名,通过函数的规则,返回最小值). l1 =[(,),(, ...
- Python基础学习Day6 is id == 区别,代码块,小数据池 ---->>编码
一.代码块 Python程序是由代码块构造的.块是一个python程序的文本,他是作为一个单元执行的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命令都是 ...
- php优化-》常用到的部分优化
1.循环内部尽可能不要声明变量: 2.在可以用PHP内部字符串操作函数的情况下,尽量不要用正则表达式: 3.foreach效率更高,尽量用foreach代替while和for循环: 4.用单引号替代双 ...