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. django-blog:多对多查询

    简单写一下多对多查询model 不是多对多的字段我就没写上来的 class Tag(models.Model): name = models.CharField(max_length=20,verbo ...

  2. Mac OS/Windows好用软件分享

    下软件全部为破解版,仅供参考学习用,如涉及商业. 请支持正版!谢谢 全部为本人亲测过 看上哪个留言发给你!   直接全分享上来会有人居心不良!

  3. 实验吧web-易-这个看起来有点简单

    看url中有id=1,明显的sql注入,这里使用手工注入不适用sqlmap,想用的话参考文章(传送门) 1.首先测试有没有sql注入漏洞, http://ctf5.shiyanbar.com/8/in ...

  4. 干货|CVE-2019-11043: PHP-FPM在Nginx特定配置下任意代码执行漏洞分析

    近期,国外安全研究员Andrew Danau,在参加夺旗赛(CTF: Capture the Flag)期间,偶然发现php-fpm组件处理特定请求时存在缺陷:在特定Nginx配置下,特定构造的请求会 ...

  5. 吴裕雄--天生自然MySQL学习笔记:MySQL 及 SQL 注入

    如果通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本章节将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入的字符. 所谓SQL注入,就是通 ...

  6. UML-测试驱动开发

    1.什么是测试驱动开发? 测试驱动开发(TDD)是迭代和敏捷XP方法提倡的优秀实践,该实践为测试测试优先开发.不仅包含单元测试. 2.为什么测试驱动开发? 1).最大提升代码稳定性 2).澄清接口和行 ...

  7. [CISCN2019 华北赛区 Day1 Web1]Dropbox-phar文件能够上传到服务器端实现任意文件读取

    0x00知识点 phar是什么: 我们先来了解一下流包装 大多数PHP文件操作允许使用各种URL协议去访问文件路径:如data://,zlib://或php://.例如常见的 include('php ...

  8. CMS-熊海网站内容管理系统漏洞测试

    开门见山 在虚拟机中搭建网站,本机访问http://192.168.31.68/ 一.SQL注入获取管理员账号密码 1. 点开一篇文章,存在get请求参数 2. 手工注入无果,使用sqlmap,后跟- ...

  9. RTMP、RTSP

    一.参考网址 1.RTMP.RTSP.HTTP视频协议详解(附:直播流地址.播放软件) 2.海康RTSP流转RTMP并推送至WEB端展示 3.使用FFmpeg将rtsp流摄像头视频转码为rtmp播放 ...

  10. netty 百度网盘 密码

    netty基础 https://pan.baidu.com/s/1v_ME49LIef1Kwga8z2QbDw?spm=a1z09.2.0.0.680b2e8d5LI8S0   zb7u mina n ...