Best Time to Buy and Sell Stock I II III
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 only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
创建差分数组,即差分数组grid[i] = price[i+1]-price[i],因此,本题可以转化为求grid中最大子数组和问题
public int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int minus[] = new int[prices.length-1];
for(int i=0;i<minus.length;i++){
minus[i] = prices[i+1]-prices[i];
}
int max = minus[0];
int now = minus[0];
for(int i=1;i<minus.length;i++){
if(minus[i]+now <minus[i]){
now = minus[i];
}else{
now = minus[i]+now;
}
if(max<now){
max = now;
}
}
return max>0?max:0;
}
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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
还是差分数组,把数组中所有正数加起来就是了。。。
public int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int max = 0;
for(int i=0;i<prices.length-1;i++){
int minus = prices[i+1]-prices[i];
max+=minus>0?minus:0;
}
return max;
}
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 (ie, you must sell the stock before you buy again).
动态规划,用两个数组记录状态,f[i]表示区间[0,i](0<=i<=n-1) 的最大利润,g[i]表示区间[i,n-1](0<=i<=n-1) 的最大利润。
public int maxProfit(int[] prices) {
if (prices.length < 2)
return 0;
int f[] = new int[prices.length];
int g[] = new int[prices.length];
for (int i = 1, valley = prices[0]; i < prices.length; ++i) {
valley = Math.min(valley, prices[i]);
f[i] = Math.max(f[i - 1], prices[i] - valley);
}
for (int i = prices.length - 2, peak = prices[prices.length - 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 < prices.length; ++i)
max_profit = Math.max(max_profit, f[i] + g[i]);
return max_profit;
}
Best Time to Buy and Sell Stock I II III的更多相关文章
- Best Time to Buy and Sell Stock I && II && III
题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
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] 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 al ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LintCode] 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 ...
- [LintCode] 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 ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- 10个工具让你的 shell 脚本更强大
10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...
- 将DataTable转化为json对象
private string DataTableTojson(DataTable dt) { List> list=new List>(); ...
- Cocoa Pods的安装
CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.幸运的是OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令: sudo gem install -n /usr/ ...
- 机房收费系统合作版(三)——UI思索
案件追踪系统1.0暂告一段落.验收过程中.MR MI针对UI界面提出了很多自己的想法. 针对TGB项目的UI设计我也有我的感受: 1.不论大小项目.仅仅要一看界面准有70%到80%熟悉度. 2.一看这 ...
- 批量创建prefab
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; using UnityEdito ...
- 学习Swift中的CoreImage(图形核心编程)
Core Image是一个可以让你轻松使用图形过虑器的强力框架.在这里你几乎可以获得所有不同种类的效果,比如修改图像饱和度,色彩范围,亮度等.它甚至也可以利用CPU或者GPU来处理图像数据并且它的速度 ...
- 连数据库是ODBC好还是OLEDB好
1.连数据库是ODBC好还是OLEDB好?2.是不是只有微软的数据库才可以用OLEDB?3.要切换这两种连接,是不是只需要修改连接字符串?谢谢大家了,小弟对这三个问题不解 分享到: 2009-03 ...
- Oracle字符集查看
Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库工具,错误消息,排序次序,日期 ...
- Js Json 互转
推荐: //js对象转换为 JSON 文本 var text = '[{"id":1,"name":"C","size" ...
- 使用HttpClient发送GET请求
HttpRequestMessage http_req_msg = new HttpRequestMessage(); http_req_msg.Method = HttpMethod.Get; ht ...