【leetcode】Best Time to Buy and Sell (easy)
题目:
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.
说白了,就是找一组数字里最大差值,并且大数要在小数的后面。 因为只能做一次买卖操作。
思路:从后向前记录最大的数和该最大数前面的最小数,不断更新最大差值。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
{
return ;
}
int maxNum = prices.back();
int minNum = prices.back();
int maxprofit = ;
vector<int>::iterator it;
for(it = prices.end() - ; it >= prices.begin(); it--)
{
if(*it < minNum)
{
minNum = *it;
}
else if(*it > maxNum)
{
maxprofit = ((maxNum - minNum) > maxprofit) ? (maxNum - minNum) : maxprofit;
maxNum = *it;
minNum = *it;
}
}
maxprofit = ((maxNum - minNum) > maxprofit) ? (maxNum - minNum) : maxprofit;
return maxprofit;
}
}; int main()
{
Solution s;
vector<int> vec;
int n = s.maxProfit(vec); return ;
}
【leetcode】Best Time to Buy and Sell (easy)的更多相关文章
- 【leetcode】Best Time to Buy and Sell 3 (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 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 III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...
- 【leetcode】Best Time to Buy and Sell 2(too easy)
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 ...
- 【数组】Best Time to Buy and Sell Stock I/II
Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...
- 【Leetcode】【Medium】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 ...
随机推荐
- SQLServer安装错误之--->无法打开项 UNKNOWN\Components\DA42BC89BF25F5BD0AF18C3B9B1A1EE8\c1c4f01781cc94c4c8fb1542c0981a2a
– 错误 1402.无法打开项 UNKNOWN\Components\7ABFE44842C12B390AF18C3B9B1A1EE8\54D21D49F3A8C1C49AC11A1B6445A83E ...
- [译]在Mac上运行ASP.NET 5
原文:http://stephenwalther.com/archive/2015/02/03/asp-net-5-and-angularjs-part-7-running-on-a-mac 这篇文章 ...
- [转载]onkeydown、onkeypress、onkeyup、onblur、o
转载链接: http://blog.sina.com.cn/s/blog_697b2dc101014ktb.html onkeydown:按下任何键(字母.数字.系统.tab等)都能触发,且对于字母不 ...
- ajax浅析---UpdatePanel
使用UpdatePanel控件 UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何 ...
- MarkDown 语法
1. 代码注释 ` ` 2.标题注释 # 3. 文章注释 ```` ```` 4.超链接 This is [an example](http://example.com/ "Title&qu ...
- 63.Hbase 常用命令
1.进入 Hbase shell ./hbase shell 2. 命令 1.list 2.truncate 3.scan 4.describe 5.create 'tablename','famil ...
- 随鼠标移动tab
<script language="javascript"> function tabChange(obj, id) { var ...
- Effective Java 学习笔记之创建和销毁对象
一.考虑用静态工厂方法代替构造器 1.此处的静态工厂方法是指返回指为类的对象的静态方法,而不是设计模式中的静态工厂方法. 2.静态工厂方法的优势有: a.使用不同的方法名称可显著地表明两个静态工厂方法 ...
- Linux之编译需要的文件变化时刻
- Android_bug之Default Activity not found
在运行自己修改默认的MainActivety,运行自己的Mainactivety时,碰到这个问题Could not identify launch activity: Default Activity ...