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

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0. 求出数列中后面的数与前面的数最大的差值;刚开始就想到了暴力解法,结果超时;随后进一步思考,在一次遍历中完成整个计算。
代码入下:
 class Solution {
public:
int maxProfit(vector<int> &prices) {
int m = prices.size();
if(prices.empty())
{
return ;
}
int curMin = prices[];
int profit = ;
for(int i = ; i < m; i ++)
{
curMin = min(curMin, prices[i]);
profit = max(profit, prices[i]-curMin);
}
return profit;
}
};
 

leetcode 121的更多相关文章

  1. [LeetCode]121、122、309 买股票的最佳时机系列问题(DP)

    121.买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意 ...

  2. 30. leetcode 121. Best Time to Buy and Sell Stock

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

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

  4. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

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

  5. leetcode 121 股票买卖问题系列

    描述: 给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大). 其他三道问题是,如果能买卖无限次,买卖两次,买卖k次. 题一: 实质是求后面一个数减前一个数的 ...

  6. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  7. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  8. Java for 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 ...

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

随机推荐

  1. 关于centos7下扩充和减小xfs逻辑分区大小的问题

    比如,我想把/dev/centos/home下的50G变为8G 1.增加 1.1 去掉挂载 umount /home 1.2 减小home, lvreduce -L 8G /dev/centos/ho ...

  2. 简单且线程安全的两个单例模式java程序

    /***具体说明见 http://www.cnblogs.com/coffee/archive/2011/12/05/inside-java-singleton.html*/ package com. ...

  3. requesting java ast from selection

    遇到這個錯誤是因為在eclipse中選擇了maven->update project.接著就不斷的出現題目上的錯誤,然後就提示是否退出workbench. 查看了一下項目的compile jre ...

  4. mongoDB索引使用

    for(var i=0;i<10000;i++){ db.user.insert({name:"user"+i,age:i}) }添加这么多数据 db.user.find({ ...

  5. iOS8 StoryBoard 连线diss方法

    添加自定义Dismiss类: //  Dismiss.h //  StoryBoardTest // //  Created by zhujin on 14/12/23. //  Copyright ...

  6. MYSQL C API : mysql_init()

    MYSQL * mysql_init(MYSQL *mysql); // 初始化一个MYSQL 连接的实例对象 void mysql_close(MYSQL *sock); // 释放一个MYSQL ...

  7. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  8. ZOJ 3407 Doraemon's Cake Machine [数学]

    题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平 ...

  9. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  10. vmstat 命令详解 转载

    vmstat 命令详解   procs:r-->在运行队列中等待的进程数b-->在等待io的进程数w-->可以进入运行队列但被替换的进程 memoyswap-->现时可用的交换 ...