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 ...
随机推荐
- 帝国cms 遍历某个父栏目下所有的子栏目
[e:loop={"select * from phome_enewsclass where bclassid in (2) order by classid asc",0,24, ...
- TypeScript 和 JavaScript 的区别
TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集.JavaScript 和 TypeSc ...
- 多线程编程-- part 5.2 JUC锁之Condition条件
1.Condition介绍 Condition的作用是对锁进行更精确的控制.Condition中的await()方法相当于Object的wait()方法,Condition中的signal()方法相当 ...
- P1:天文数据获取
Step1:在sloan的casjob里http://casjobs.sdss.org/CasJobs/,密码用户 jiangbin 123456 查询满足条件的光谱对象,得到光谱对象的plate, ...
- postgres日常操作
1.启动pgsl数据库 [postgres@master ~]$ pg_ctl start [postgres@master data]$ pg_ctl -D /usr/local/pgsql/dat ...
- windows环境变量和相关命令操作
1.很多程序在windows上运行都需要设置环境变量. 2.具体步骤 复制路径 打开系统设置 高级系统设置 环境变量 设置path 重启cmd 3.可以把路径设置成变量,这样就不用随时 改path而是 ...
- C# 过滤字典中的数据 并将过滤后的数据转成新的字典对象
Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("); dic. ...
- Java泛型:利用泛型动态确认方法返回值类型
根据泛型类型动态返回对象 public <T extends PackObject> T unPackMessage(String interfaceCode, String respVa ...
- SQL函数取汉字拼音首字母
)='') ) as begin ), ) , ,) if @chn > 'z' if( @chn < '八' ) set @c = 'A' else if ( @chn < '嚓' ...
- Java定时任务的几种方法(Thread 和 Timer,线程池)
/** * 普通thread * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着, * 通过sleep方法来达到定时任务的效果.这样可以快速简单的实现,代码如下: * */ ...