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 ...
随机推荐
- Tomcat部署项目通过—IP地址:端口访问
如题所示,实现效果图如下: 设置如下: (1)修改${tomcat}/config/sever.xml文件虚拟内容目录: <Engine name="Catalina" de ...
- QQ聊天界面的布局和设计(IOS篇)-第二季
QQChat Layout - 第二季 本来第二季是快写好了, 也花了点功夫, 结果gitbook出了点问题, 给没掉了.有些细节可能会一带而过, 如有疑问, 相互交流进步~. 在第一季中我们完成了Q ...
- java 实现排序
package com.cjs.sort; /** * 此类用来提供针对整数的各种排序算法 * * @author S * @version 1.0 */ public class MySort { ...
- Android学习笔记__1__Android体系架构
Android 体系结构图 Android作为一个移动设备的平台,其软件层次结构包括了一个操作系统(OS),中间件(MiddleWare)和应用程序(Application).根据Android的软件 ...
- ubuntu 右键新建文档
第一步.创建一个空白doc文档. 一定要是空白的,然后保存(另存为)到桌面,注意细节:1.保存到你能找到的地方,最好是桌面,以方便第二步拖动 2.注意下方的 “文件类型”,要选择doc格式. 3.注意 ...
- [Immutable.js] Differences between the Immutable.js Map() and List()
The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-valu ...
- 适用于CSS2的各种运动的javascript运动框架
<script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...
- overflow清楚浮动 + 去掉li标签的小圆点
原文链接:http://blog.163.com/qqabc20082006@126/blog/static/22928525201031211212955/ 测试用例: <!DOCTYPE h ...
- C# 4.0 并行计算部分
C# 4.0 并行计算部分 c#linq算法多线程list微软 目录(?)[-] C 40 并行计算部分 一简单使用 二 并行循环的中断和跳出 三并行循环中为数组集合添加项 四返回集合运算结果 ...
- Xcode 常用快捷键及代码自动排版
1. 文件CMD + N: 新文件CMD + SHIFT + N: 新项目CMD + O: 打开CMD + S: 保存CMD+OPt+S:保存所有文件CMD + SHIFT + S: 另存为CMD + ...