[LeetCode]题解(python):121-Best Time to Buy and Sell Stock
题目来源:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
题意分析:
给定一个数组,代表array[i] 代表第i天的价格。问买买卖这个物品一次的最高利润是多少(i买,j卖,j > i)。
题目思路:
记录当前最小值,如果array[i] < min,那么更新min,否者计算如果在i天的卖的利润,和当前最大利润比较。
代码(python):
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
ans,mins = 0,prices[0]
for i in prices:
if i > mins:
ans = max(ans,i - mins)
else:
mins = i
return ans
[LeetCode]题解(python):121-Best Time to Buy and Sell Stock的更多相关文章
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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 ...
- [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 ...
- 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 ...
- 【刷题-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 ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
随机推荐
- html学习笔记一
学习了一天,总结巩固下自己收获. html是超文本标记语言,而不是编程语言. 1:html结构 包含html标签,head标签,title标签,body标签. <html> <hea ...
- js 使用for循环遍历数组
今天写个无聊的东西!for循环的使用! 例如以下:定义a数组,b为伪数组! var a = [1,2,3,0,5,4]; var b = document.getElementsByTagName(' ...
- Foundation 学习
官网 Foundation是个跟bootstrap齐名的前端框架. 移动优先,响应式,最低支持IE8. html+css+jq构建 网格Grid Basic: .row父容器 子元素类.column ...
- 权威指南之脚本化jquery
jqury函数 jquery()($())有4种不同的调用方式 第一种是最常用的调用方式是传递css选择器(字符串)给$()方法.当通过这种方式调用时,$()方法会返回当前文档中匹配该选择器的元素集. ...
- Ext.Net 使用总结之GridPanel中的选中行
1.判断GridPanel中是否选中了某行 if (!GridPanel1.hasSelection()) { Ext.Msg.alert("提示", "请选择记录!&q ...
- MyEclipse 2013 导入MyEclipse 9.0的EJB项目时,需要注意
点击“next”按钮,出现下面的对话框: 再点击“next”按钮,出现下面的对话框:
- ubuntu下浏览器调用本地应用程序
ubunut下浏览器调用本地应用程序需要desktop文件和scheme协议的支持,和windows 的url protocol类似,只是注册协议的方式不一样. 首先是desktop文件,里面需要加入 ...
- USACO Section 3.3 Camlot(BFS)
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
- YII与Ace Admin 的集成
目录 一. 前言... 1 二.为什么要使用YII+ace. 1 三.新建YII模块... 1 四.如何修改模板... 3 五.注意的地方... 4 六.整合的不足之处... 4 一. 前言 yii- ...
- eclipse 搭建Robotium环境--apk 环境搭建
1.配置好android sdk ,java环境 2.重新签名apk文件 在用户目录下,会有一个.android的目录,把re-sign.jar放在该目录下.执行命令 java -jar re-sig ...