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 1:

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.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
 class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if prices is None or len(prices) <= 1:
return 0
min_num, res = prices[0], -1
for num in prices:
if num < min_num:
min_num = num
if num - min_num > res:
res = num - min_num
return res

[LC] 121. Best Time to Buy and Sell Stock的更多相关文章

  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. 121. Best Time to Buy and Sell Stock@python

    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 买卖股票的最佳时间

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

  7. 【刷题-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 ...

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

随机推荐

  1. maven中的groupId和artifactId 区分

    原文地址:https://blog.csdn.net/snowin1994/article/details/53024871/ maven中的groupId和artifactId 区分 groupid ...

  2. 对PHP-GC(垃圾回收)的一点理解

    一直对php的垃圾回收机制不明不白故自己开贴记录下. 首先,说到垃圾回收,得先知道什么情况下才能产生垃圾. 如果一个变量refcount在增加,说明在被使用,不是垃圾. 如果一个变量的refcount ...

  3. 201771010123汪慧和《面向对象程序设计Java》第二周学习总结

    一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...

  4. es和数据库关系对比

    es类比传统关系型数据库:   Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -& ...

  5. 记录一次URL中有特殊字符怎么处理?

    你out了,赶紧换 RestTemplate 吧! 进入正题,直接实战!!! import java.util.HashMap; import java.util.Map; import org.ju ...

  6. 第 36 章 TCP/IP协议基础

    问题一:为什么要有缓存表?为什么表项要有过期时间而不是一直有效 1.参考网址: 1)网络——ARP协议 2)linux arp机制解析 2.解答: 2.1 ARP缓存可以减小广播量,当主机发送一个AR ...

  7. 饭卡(DP)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额. 如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大 ...

  8. PID<->Port[转]

    //Netstat -anb #include <Windows.h> #include "Psapi.h" #include <Iprtrmib.h> # ...

  9. 正则表达式匹配字符串中的数字 Python

    1.使用“\d+”匹配全数字 代码: import re zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650." m = ...

  10. dubbo的灰度发布

    1,什么是灰度发布 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用. 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为 ...