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 ...
随机推荐
- 90. Subsets II (Java)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 免费使用Google
这里需要借助一下`梯子`,这里有教程 点击进入 如果没有谷歌浏览器,进入下载最新版谷歌浏览器,进入下载,不要移动它的安装位置,选择默认位置, 如果已经安装了谷歌浏览器,打开赛风之后,选择设置 进行安装 ...
- linux 指定ftp用户 特定目录及权限
Linux添加FTP用户并设置权限 在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建 ...
- Java并发(基础知识)—— Executor框架及线程池
在Java并发(基础知识)—— 创建.运行以及停止一个线程中讲解了两种创建线程的方式:直接继承Thread类以及实现Runnable接口并赋给Thread,这两种创建线程的方式在线程比较少的时候是没有 ...
- python 教程之Django(二)
官网: https://www.djangoproject.com/download/ 1.简单方法: A.pip 命令安装方法 pip install Django 打开dos命令窗口 输入命令回车 ...
- java将一数组乱序排列
JAVA的Collections类中shuffle方法模拟了“洗牌”动作可以对list列表进行随机排序.如果一定要自己写,算法也很简单:假设数组array长度为n.用标准随机函数rand(n)生成[0 ...
- vue iOS上传图片file 出错
前言 用vue 移动端上传图片在低版本的 ios 手机上 图片转换base64 在转换file 文件类型 会报错 并且报错 “Script Error ” 查阅了github 和一些文档发现 可以吧 ...
- 【LuoguP5383】[模板]普通多项式转下降幂多项式
传送门 Sol (怎么老是有人喜欢出新的多项式毒瘤板子,懒得整到一起了) 核心就是把 幂用下降幂来代替. 使用斯特林数展开幂为下降幂: \[x^n=\sum_{i=0}^n{x\choose i}i! ...
- [洛谷P2605] ZJOI2016 基站选址
问题描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...
- sqlserver字段选择参照
SQL SERVER提供的说明. bit:0或1的整型数字 int:从-2^31(-2,147,483,648)到2^31(2,147,483,647)的整型数字 smallint:从-2^15( ...