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 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.
分析
求买卖最大收益问题。简单来说就是给定一个整数序列,求最大差值。
该题目的关键是,效率。
我们很容易想到二次循环解决该问题,但是很不幸,是超时的! 所以,必须另择他法。
其实在O(n)时间内,也可以解决问题,只需要加一个记录当前最低价格的变量即可;价低购入,价高售出嘛~
AC代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
//maximum记录最大收益,price记录当前最低价格
int maximum = 0, price = prices[0] ,size = prices.size();
for (int i = 1; i < size ; ++i)
{
//价低时买入
if (prices[i] < price)
{
price = prices[i];
continue;
}
//价高时卖出,比较并记录最大收益
else{
int tmp = prices[i] - price;
if (tmp > maximum)
maximum = tmp;
}
}//for
return maximum;
}
};
LeetCode(121) Best Time to Buy and Sell Stock的更多相关文章
- 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 ...
- 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 ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- <LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode(121):买卖股票的最佳时机
Easy! 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买 ...
- LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
Description Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode 122]买股票II 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 ...
- [LeetCode&Python] Problem 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 ...
- LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...
随机推荐
- On the way to the park Gym - 101147I 几何
http://codeforces.com/gym/101147/problem/I I. On the way to the park time limit per test 5 seconds m ...
- GIT GUI克隆github代码
新建一个文件夹,右击gitgui git clone 去掉不要
- Nginx 开启目录浏览功能配置
在server节点下添加 server { listen ; server_name default; #index index.php; # 目录浏览功能 autoindex on; # 显示文件大 ...
- VS局域网断点调试设置
1.电脑文档文件夹下\IISExpress\config文件内找到applicationhost.config文件编辑 找到<sites>节点 找到你要编辑的site节点 在<bin ...
- 1058 合唱队形 2004年NOIP全国联赛提高组
1058 合唱队形 2004年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descripti ...
- jQuery1.6.1源码分析系列(作者:nuysoft/高云)
作者:nuysoft/高云 QQ:47214707 Email:nuysoft@gmail.com jQuery源码分析(版本1.6.1) 00 前言开光 01 总体架构 02 正则表达式-RegEx ...
- MAC无法确认开发者身份
网上下载的软件,如果来自身份不明的开发者,在MAC上打开时会提示无法确认开发者的身份,在网上找到了一篇尝试解决的文章,文章链接地址为http://jingyan.baidu.com/article/f ...
- bsub && lsf 介绍
文章转载地址:http://www.bbioo.com/lifesciences/40-114265-1.html LSF系统介绍 http://scc.ustc.edu.cn/zh_CN/ 中科大超 ...
- jquery 不支持$.browser
if (!$.browser) { $.browser = { mozilla : /firefox/.test(navigator.userAgent.toLowerCase()), webkit ...
- decompressedResponseImageOfSize:completionHandler:]_block_invoke
原因: It turns out the linker error was caused by the CGImageSourceCreateWithData call. And the root ...