mycode  70.94%

思路:其实没必要去考虑在计算了一个max-min后,后面又出现了一个新的的最小值的情况,因为res取值就是取自己和新的res的最大值

在遇见max值之前,遇见新的最小值,直接更新最小值

在遇见max值之后,遇见新的最小值,没关系啊,因为res已经记录了前面最大值和最小值之间的差啦,只要新的最小值之后能够找到新的最大值使得新的差值比res大,就去更新res,否则res不变啦

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
m = float('inf')
n = 0
pos1 = 0
pos2 = 0
res = 0
for i in range(len(prices)):
if prices[i] < m :
pos1 = i
m = prices[i]
if prices[i] > m and i > pos1:
res = max(res,prices[i]-m)
return res

简化  77.91%

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
m = float('inf')
res = 0
for i in range(len(prices)):
if prices[i] < m :
m = prices[i]
if prices[i] > m :
res = max(res,prices[i]-m)
return res

参考

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices)<2:
return 0
lowest = prices[0]
rst = 0
for price in prices:
if price<lowest:
lowest = price
rst = max(rst, price-lowest)
return rst

leetcode-easy-dynamic-121 Best Time to Buy and Sell Stock的更多相关文章

  1. 【leetcode❤python】121. Best Time to Buy and Sell Stock

    #-*- coding: UTF-8 -*- #Method1 :超时#class Solution(object):#    def maxProfit(self, prices):#      # ...

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

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

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

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

  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. LeetCode(122) Best Time to Buy and Sell Stock II

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

  9. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

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

随机推荐

  1. 帝国cms 反馈

    <form name='feedback' method='post' enctype='multipart/form-data' action='/e/enews/index.php' ons ...

  2. Python(os和sys)理解

    Python(os和sys)理解 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时 ...

  3. MyCat配置简述以及mycat全局ID

    Mycat可以直接下载解压,简单配置后可以使用,主要配置项如下: 1. log4j2.xml:配置MyCat日志,包括位置,格式,单个文件大小 2. rule.xml: 配置分片规则 3. schem ...

  4. cmd完成拷贝文件,并生成两个快捷脚本

    @echo off@echo ------------------------------ @echo 正在创建目录 color 03if exist y:\00程序数据备份 ( md y:\00程序 ...

  5. LeetCode_Bit Manipulation

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  6. xss技巧记录

    1.iframe的srcdoc属性 先演示一下 <iframe srcdoc="<script>alert(1)</script>"> 浏览器渲 ...

  7. java8学习之收集器枚举特性深度解析与并行流原理

    首先先来找出上一次[http://www.cnblogs.com/webor2006/p/8353314.html]在最后举的那个并行流报错的问题,如下: 在来查找出上面异常的原因之前,当然得要一点点 ...

  8. uestc summer training #2

    A 增广 #include<bits/stdc++.h> using namespace std; + ; vector<int> g[MAXN]; int a[MAXN], ...

  9. windows时钟服务设置

    运行Regedit,打开注册表编辑器. 找到注册表项HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\,将Anno ...

  10. Kattis - bitwise Bitwise (RMQ+尺取+树上dfs)

    题意:有一个长度为n的序列,让你把它分成k段,段内元素取or,段间取and,求能够得到的最大值. 这个算法是我和xz场上yy出来的,然而时间不够了没写出来,而且时间复杂度是$O(nlogn+nlogA ...