best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值
1、
Say you have an array for which the i th 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.
给一个数prices[],prices[i]代表股票在第i天的售价,求出只做一次交易(一次买入和卖出)能得到的最大收益。
只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o....i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return ;
int res=,min=prices[];
for(int i=;i<prices.size();i++){
if(prices[i]<min) min=prices[i];
else if(prices[i]-min>res) res=prices[i]-min;
}
return res;
}
};
2、
Say you have an array for which the i th 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).
此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int res =;
for(int i=;i<prices.size();i++){
if(prices[i]-prices[i-]>)
res+=prices[i]-prices[i-];
}
return res;
}
};
3、
Say you have an array for which the i th 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.
此题是限制在两次交易内,相对要难一些。容易想到的解决办法是,把prices[] 分成两部分prices[0...m] 和 prices[m...length] ,分别计算在这两部分内做交易的做大收益。由于要做n次划分,每次划分可以采用 第一题: I的解法, 总的时间复杂度为O(n^2).
public class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for(int m = 0; m<prices.length; m++){
int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
if(tmp > ans) ans = tmp;
}
return ans;
}
public int maxProfitOnce(int[] prices,int start, int end){
if(start >= end) return 0;
int low = prices[start];
int ans = 0;
for(int i=start+1; i<=end; i++){
if(prices[i] < low) low = prices[start];
else if(prices[i] - low > ans) ans = prices[i] - low;
}
return ans;
}
}
但是由于效率过低,运行超时。可以利用动态规划的思想进行改进,保持计算的中间结果,减少重复的计算。
那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。计算方法也是利用第一个问题的计算方法。 第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。 所以最后算法的复杂度就是O(n)的。
就是说,通过预处理,把上面的maxProfitOnce()函数的复杂度降到O(1)
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int n=prices.size();
vector<int> opt(n,);
int res=,low=prices[];
for(int i=;i<n;i++){
if(prices[i]<low) low=prices[i];
else if(res <prices[i]-low) res=prices[i]-low;
opt[i]=res;
}
vector<int> optReverse(n,);
int high=prices[n-];
res=;
for(int i=n-;i>=;i--){
if(prices[i]>high) high=prices[i];
else if(high-prices[i]>res) res=high-prices[i];
optReverse[i]=res;
}
res=;
for(int i=;i<n;i++){
int tmp=opt[i]+optReverse[i];
res=tmp>res?tmp:res;
}
return res;
}
};
best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值的更多相关文章
- Best Time to Buy and Sell Stock系列
I题 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- LeetCode -- Best Time to Buy and Sell Stock系列
Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【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] 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 IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
随机推荐
- 30行js让你的rem弹性布局适配所有分辨率(含竖屏适配)
用rem来实现移动端的弹性布局是个好主意!用法如下: CSS @media only screen and (max-width: 320px), only screen and (max-devic ...
- Leetcode 481.神奇字符串
神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...
- android.os.NetworkOnMainThreadException解决办法
代码改变世界 在发起Http请求的Activity里面的onCreate函数里面添加如下代码: StrictMode.setThreadPolicy(new StrictMode.ThreadPoli ...
- kali2 install Nessus
注册: https://www.tenable.com/products/nessus-home 安装: 设置登录用户名,密码,输入注册码:
- jenkins配置本机JDK和maven环境
1.jenkins官网下下载jenkins的war包 2.安装jenkins,启动命令:java -jar jenkins.war 3.打开http://localhost:8080/ 4.点击系统 ...
- zabbix基于LNMP安装
安装依赖 yum install pcre* #为了支持rewrite功能 yum install openssl openssl-devel yum install gcc make gd-deve ...
- 关于PHP xss 和 SQL 注入的问题
漏洞无非这么几类,XSS.sql注入.命令执行.上传漏洞.本地包含.远程包含.权限绕过.信息泄露.cookie伪造.CSRF(跨站请求)等.这些漏洞不仅仅是针对PHP语言的,PHP如何有效防止这些漏洞 ...
- 如何应用r.js对requirejs下的js代码合并
1.在根目录新建build.js ({ baseUrl:'js', paths:{ jquery:'static/jquery-1.10.2.min', underscore:'static/unde ...
- 转 Python爬虫入门二之爬虫基础了解
静觅 » Python爬虫入门二之爬虫基础了解 2.浏览网页的过程 在用户浏览网页的过程中,我们可能会看到许多好看的图片,比如 http://image.baidu.com/ ,我们会看到几张的图片以 ...
- Android添加永不休眠选项
很多朋友在使用TQ210做项目的时候都需要屏幕保持长亮,本文介绍如何修改TQ210 android源码,使系统可以永不休眠,代码如下: 1.添加英文选项 修改packages/apps/Setting ...