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 ...
随机推荐
- yii的rules验证规则
图片验证 public function rules() { return [ [['id'], 'integer'], [['id'], 'required'], [['files'], 'file ...
- vue--微信支付
1.当前页面没有注册: 需要登录微信支付商家后台,添加支付路径授权 2.安装 weixin-js-sdk 微信SDK npm install weixin-js-sdk --save 3.引入 imp ...
- Java注解的继承
注解继承的说明 1.首先要想Annotation能被继承,需要在注解定义的时候加上@Inherited,并且如果要被反射应用的话,就需要还有个事@Retention(RetentionPolicy.R ...
- Linux中FTP服务器配置
1.FTP服务器的一些基本概念 (1)FTP连接方式 控制连接:标准端口为21,用于发送FTP命令信息. 数据连接:标准端口为20,用于上传.下载数据. ...
- Codeforces Round #344 (Div. 2) 631 C. Report (单调栈)
C. Report time limit per test2 seconds memory limit per test256 megabytes inputstandard input output ...
- Summer training round2 #10(Training 30)
A:签到题 B!:搜索+DP #include<bits/stdc++.h> #define mp make_pair #define pi pair<int,int> usi ...
- Hibernate的缓存(收集)
(1)缓存就是把以前从数据库中查询出来和使用过的对象保存在内存中(一个数据结构中),这个数据结构通常是或类似Hashmap,当以后要使用某个对象 时,先查询缓存中是否有这个对象,如果有则使用缓存中的对 ...
- local_time
time_t time(time_t *tloc); 功能:获取纪元1970-01-01 00:00:00以来所经历的秒数 参数: tloc:用来存储返回时间 返回值:成功:返回秒数, 失败:-1 - ...
- redis的数据类型和基本操作
Redis 的Key Redis 的 key 是字符串类型,但是 key 中不能包括边界字符,由于 key 不是 binary safe的字符串,所以像"my key"和" ...
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...