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 ...
随机推荐
- idea中deployment点击加号没有出现artifact
转载 在主页面打开ProjectStructure,点击图示的按钮或是按ctrl+shift+alt+s快捷键 打开ProjectStructure后,按照图示依次点击Facets->+号,在弹 ...
- jQuery取得/设置select的值
本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...
- MySQL数据库报错:Too many connection
每次搭建环境运行一段时间,后台就会报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data sourc ...
- sourceTree免注册免登陆使用方法-Windows
安装sourceTree需要注册Google账号,而现在国内注册账号需要FQ,超级麻烦,所以还是免注册的号. 处理方法: 解决办法 在目录C:\Users\{youruser}\AppData\Loc ...
- 利用樹霉派採集溫濕度上傳到OneNET(非完整,僅參考)
看圖: Python代碼: #env /usr/bin/python3 #author Bruce import RPi.GPIO as GPIO import time import json im ...
- Java中的数据类型——通过示例学习Java编程(5)
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=15 数据类型用来定义变量可以采用的值,例如,如果变 ...
- Java基础教程(25)--I/O
一.I/O流 I/O流表示输入源或输出目标.流可以表示许多不同类型的源和目标,例如磁盘文件.设备.其他程序等. 流支持许多不同类型的数据,包括字节.原始数据类型.字符和对象等.有些流只传递数据 ...
- 快速搭建一个Fabric 1.0的环境(转)
文章来源:http://www.cnblogs.com/studyzy/p/7437157.html 感谢博主@深蓝居 提供的技术文档,按照文档根据自己遇到的问题做了一定修改,方便自己回顾和再次搭建 ...
- Eclipse-运行符-数据类型转换-环境变量配置
1.能够使用Eclipse快捷键 ctrl + / 单行注释:再按一次则取消: ctrl + shift + / 多行注释: ctrl + shift + \ 取消多行注释: ctrl + ...
- Jquery AJAX使用踩坑小记
在使用jquery ajax时,如果其参数是一个json对象,将此参数使用$('#dd').data(param)绑定到一个元素上, 在使用$('#dd').bind('click',function ...