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.

We solve the problem by examples:

Suppose the input array is: [2, 1, 3, 4, 5, 4]. 

Then, the corresponding diagram is as the following:

             5

            / \

       4   /   4

      / \ /

     3   3

2   /

 \ /

  1



So, what we actually have to do is to find the difference value between the max and the min.

int maxProfit(vector<int> &prices) {
if(prices.size() == 0)return 0; int min = prices[0];
int max = 0;
for(int i = 0; i < prices.size(); ++i)
{
if(prices[i] - min > max)
max = prices[i] - min; if(prices[i] < min)
min = prices[i];
} return max;
}

Leetcode之Best Time to Buy and Sell Stock的更多相关文章

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

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

  3. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] 309. 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 ...

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

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

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

随机推荐

  1. hdu 1171 Big Event in HDU(01背包)

    代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; in ...

  2. 《从0到1》读书笔记第2章&quot;像1999 年那样狂欢&quot;第1记:小结及词汇解析

    小结 本章的目的应该是通过90年代末的互联网泡沫的背景,成因.影响,以及教训来教诫人们,在全部人都疯狂的抛身于洪流热潮之中时,我们要冷静的思考辨识出那些不切实际的大众观点,找到隐藏在这些观点后面的反主 ...

  3. TextWatcher-监听输入框内容变化

    今天在做邮件登录的时候,遇到了输入框(Edittext)监听事件(TextWatcher),现在记录下. 首先看如下代码 eText.addTextChangedListener(new TextWa ...

  4. BZOJ 2037 区间DP

    跟POJ 3042是一个类型的http://blog.csdn.net/qq_31785871/article/details/52954924 思路: 先排个序 (把初始位置也插进去) f[i][j ...

  5. pix格式的摸索(二)

    作者:朱金灿 来源:http://blog.csdn.net/clever101 PCI的系统格式pix是一个设计很巧妙的遥感图像格式,而且其设计巧妙之处不止一处两处,这些都有待我日后一一去摸索.今天 ...

  6. zookeeper提供了什么

    简单的说,zookeeper=文件系统+通知机制. 每个子目录项如 NameService 都被称作为 znode,和文件系统一样,我们能够自由的增加.删除znode,在一个znode下增加.删除子z ...

  7. 《深入理解java虚拟机》:类的初始化

    深入理解java虚拟机>:类的初始化 类从被载入到虚拟机内存中開始.到卸载出内存为止,它的整个生命周期包含:载入.验证.准备.解析.初始化.使用和卸载七个阶段.当中验证.准备.解析3个部分统称为 ...

  8. c、c++ 结构体的嵌套

    c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...

  9. cocos2dx-js学习笔记(一)环境搭建

    本人眼下的学习方向是cocos2dx+js的开发方式,开发调试使用webstrom和火狐浏览器,调试完毕的项目使用cocos2dx+jsb的方式编译到PC或android设备执行.主要时间用在学习,所 ...

  10. BZOJ2882

    传送门:BZOJ2882(权限题) 最小表示法的模板. 传送门:周神论文 代码上的小细节见下. #include <cstdio> #include <cstdlib> #in ...