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. Codevs 1976 Queen数列

    1976 Queen数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 将1到N的整数数列(1,2,3,… ...

  2. uoj#418. 【集训队作业2018】三角形(线段树合并)

    传送门 好迷啊--膜一下ljz 考虑每个操作,如果把操作按先后顺序放到序列上的话,操作一就是把\(w_i\)的石子放到某个节点,那么就是在序列末端加入\(w_i\),然后根据贪心肯定要把它所有儿子的石 ...

  3. 新装centos 6.5 基本配置

    开机自动联网 vi /etc/sysconfig/network-scripts/ifcfg-eth0; 将ONBOOT=no,改为ONBOOT=yes,保存退出 开机直接进入命令行模式 vi /et ...

  4. TensorFlow数据集(一)——数据集的基本使用方法

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 例子:从一个张量创建一个数据集,遍历这个数据集,并对每个输入输出y = x^2 的值. #!/usr/bin/en ...

  5. dream(2018.10.17)

    一句话题意:讲什么题意啊,见usaco2017feb silver的T1,一模一样,就是牛和鸡的读入顺序反过来了一下,数据范围大了10倍,卡掉了\(O(n^2)\)的算法. 数据范围: 对于 30%的 ...

  6. day01 继承

  7. android intent安装apk

    /** * 安装apk * * @param context * @param apkPath */ public static void installApk(Context context, St ...

  8. spring+mybits 整合所需jar包的下载路径(亲测有效)

    1.spring jar包:http://repo.springsource.org/libs-release-local/org/springframework/spring/5.0.0.RELEA ...

  9. Gym - 101810D ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...

  10. CVE-2017-5638——S2-045

    一. 漏洞简介 Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用于创建企业级Java Web 应用的开源MVC框架,主要提供两个版本框架产品: Stru ...