题目:

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)的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. 【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 ...

  4. 【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 ...

  5. 【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 ...

  6. 【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 ...

  7. 【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 ...

  8. 【数组】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 ...

  9. 【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 ...

随机推荐

  1. C#2.0 特性

    泛型 迭代器 分布类 可空类型 匿名方法 命名空间别名限定符 静态类 外部程序程序集别名 属性访问器可访问性 委托中的协变和逆变 如何声明.实例化.使用委托 固定大小的缓冲区 友元程序集 内联警告控制 ...

  2. Linux 开机启动方式设置 inittab 详解,开机直接进入“命令行”模式

    Linux下的 /etc/inittab 中的英文解释: This file describes how the INIT process should set up  the system in a ...

  3. LAMP平台部署及应用

    环境:http://www.cnblogs.com/zzzhfo/p/5925786.html  http://www.cnblogs.com/zzzhfo/p/5934630.html 1.LAMP ...

  4. Unity 用户手册用户指南二维纹理 (Texture 2D)

    http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...

  5. 使用json存储结构化数据

    从文件中读写字符串很容易.数值就要多费点儿周折,因为read ()方法只会返回字符串,应将其传入int()这样的函数,就可以将'123'这样的字符串转换为对应的数值 123.当你想要保存更为复杂的数据 ...

  6. python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940

    1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...

  7. BZOJ 4531: [Bjoi2014]路径

    Description 一个无向图,每个节点有一个字符,问形成长度为k的的合法表达式的方案数. Sol DP. \(f[i][o][p][0/1]\) 表示走 \(i\) 步,到 \(o\) ,有 \ ...

  8. net-snmp源码VS2013编译添加加密支持(OpenSSL)

    net-snmp源码VS2013编译添加加密支持(OpenSSL) snmp v3 协议使用了基于用户的安全模型,具有认证和加密两个模块. 认证使用的算法是一般的消息摘要算法,例如MD5/SHA等.这 ...

  9. 38 网络相关函数(六)——live555源码阅读(四)网络

    38 网络相关函数(六)——live555源码阅读(四)网络 38 网络相关函数(六)——live555源码阅读(四)网络 简介 12)makeSocketNonBlocking和makeSocket ...

  10. [黑科技]bit reverse

    写FFT的时候yy出来了这个bit reverse...时间复杂度O(n),常数大概是(a[x>>1]>>1)|((x&1)<<26)的二分之一(-O3下) ...