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. POJ 1006 Biorhythms (数论-中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111285   Accepted: 34638 Des ...

  2. 用c#编写爬虫在marinetraffic下载船仅仅图片

    近期在做船仅仅识别方面的事情,须要大量的正样本来训练adaboost分类器. 于是到marinetraffic这个站点上下载船仅仅图片.写个爬虫来自己主动下载显然非常方便. 站点特点 在介绍爬虫之前首 ...

  3. RvmTranslator6.5 is released

    RvmTranslator6.5 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...

  4. Http post提交和get提交

    public string  PostParameter(string url)         {  string message="";             GetOrde ...

  5. Linq查询案例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 缓存函数memorize

    function mulity(x){ return x*x; } function memorize(f){ var cache = {}; var key = arguments.length + ...

  7. 63.C++异常

    #include <iostream> using namespace std; //异常与错误不一样,异常一般能正常工作 //错误就是程序无法正常工作,无法编译 //异常让程序在错误的输 ...

  8. touch---创建文件或更改文件日期

  9. mapper提示Could not autowire. No beans of … type found?

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql 在自动生成工具生成代码后,serv ...

  10. 微软推送Win10致全球网络负担增大,中国网友表示毫无压力

        7月29日晚间,微软的Windows10操作系统正式上市发行.微软将面向全球190个国家地区进行windows10的免费升级推送. 眼下, Windows 10 正以疯狂的速度登陆全球各类设备 ...