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. 76.Nodejs Express目录结构

    转自:https://blog.csdn.net/xiaoxiaoqiye/article/details/51160262 Express是一个基于Node.js平台的极简.灵活的web应用开发框架 ...

  2. 网管软件 LANDesk的配置(视频配截图)

    网管软件 LANDesk Server Manager 8.5的配置截图         LANDesk桌面管理套件是应用于大中型企业环境下的计算机管理的最佳解决方案.它提供了从计算机资产管理.软件分 ...

  3. Vectorized implementation

    Vectorization Vectorization refers to a powerful way to speed up your algorithms. Numerical computin ...

  4. Gym 100952 B. New Job

    B. New Job time limit per test 1 second memory limit per test 64 megabytes input standard input outp ...

  5. DG应用日志相关操作

    应用archive log: recover managed standby database disconnect;     应用redo logfile: recover managed stan ...

  6. Android模拟、实现、触发系统按键事件的方法

     Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...

  7. activity-启动动画的设定(下面弹出出现,弹入下面消失)

    1.今天为了把一个activity以dialog的形式显示,而且实现从开始的时候从底部往上弹出,结束的时候,从上往下消失,做了如下的工作. 1)如果把一个activity以dialog的形式显示? 这 ...

  8. Mysql基本增删改查

    1登陆服务器 mysql -h localhost -u username -p password 2查看存在数据库 show databases; 3创建一个数据库(例如名字为class1,以下都是 ...

  9. mysql 压力测试之批量插入自增字段不连续问题

    Gaps in auto-increment values for “bulk inserts” With innodb_autoinc_lock_mode set to 0 (“traditiona ...

  10. Java学习笔记三.3

    9.异常处理:Java中的异常处理对象就是将以前的if语句进行的判断进行抽象化,并形成的一套错误处理体系.最顶端是Throwable,接着是Error,Exception,其中Exception又明显 ...