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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

题目地址: Best Time to Buy and Sell Stock

难度: Easy

题意: 买卖股票,最多只能买卖一次

思路:

遍历数组,找到最小的价格,并且每一个值与最小值比较,找出最大值, 如上面例子:

[7,1,5,3,6,4] --> [7,1,5,3,6,4] --> [7,,5,3,6,4] --> 
[7,,5,3,6,4] --> [7,,5,3,6,4]--> [7,,5,3,6,4]

代码:

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n <= 1:
return 0
res = 0 small = prices[0]
for i in range(n):
if prices[i] < small:
small = prices[i] # 最小值
res = max(res, prices[i]-small)
return res

时间复杂度: O(n)

空间复杂度: O(1)

121. Best Time to Buy and Sell Stock@python的更多相关文章

  1. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

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

  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 、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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  4. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

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

  6. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

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

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

  8. (Array)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 ----- java

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

随机推荐

  1. sed 匹配\n换行符

    假设 str="a,b,c,d" echo ${str} | sed "s/,/\n/g" 输出: a b c d echo ${str} | sed &quo ...

  2. mybatis-trim标签说明

    trim标签使用1.trim 有四个属性 2.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides ...

  3. C - 不要62

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  4. HDU - 6312( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6312 输出前几项,都是"Yes" #include <bits ...

  5. [POJ1463] Strategic game

    题目链接: 传送门 题目大意: Bob非常享受玩电脑游戏的过程,尤其是策略游戏,但是在有些时候,他因为不能在第一时间找到最佳的策略而十分伤心. 现在,他遇到了一个问题.他必须保卫一个中世纪的城市,有很 ...

  6. 安装好的php独立添加扩展模块

    在装好php后,或者在使用php的时候,发现某个模块没有添加,而又不想重新编译安装,这时就需要单独添加扩展模块. php环境说明: 安装路径:/data/php5.6/ 解压路径:/data/php- ...

  7. G. Of Zorcs and Axes 二分 + 贪心 —— STL的用法

    http://codeforces.com/gym/101149/problem/G 一开始还以为要用二分图去做,但是复杂度也太高了,O(n * m)的话直接爆炸. 考虑贪心,考虑第i个东西优先选一个 ...

  8. 系统讲解一下,Dao,Entity,Servlet,Action各自有什么东西-Java/Web开发

    dao 主要是一些接口,里面定义了一些用于增删改查的方法名 daoImpl 就是对dao的具体实现 Service 同上,也是一些接口,主要是用来调用dao层的一些方法,所以这里定义的方法一般都定义好 ...

  9. Negut 上传乱码

    解决办法 修改 bat  文件的 格式为ANSI格式即可

  10. intelliJ idea 下载安装

    Intellij IDEA是公认的java开发最好的工具,必须学会. 1. 打开网址 https://www.jetbrains.com/ 2. 点击 Intellij IDEA 图标连接,如下图 3 ...