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 only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
题目地址: Best Time to Buy and Sell Stock
难度: Easy
题意: 买卖股票,最多只能买卖一次
思路:
遍历数组,找到最小的价格,并且每一个值与最小值比较,找出最大值, 如上面例子:
[7,1,5,3,6,4] --> [7,1,5,3,6,4] --> [7,,5,3,6,4] -->
[7,,5,3,6,4] --> [7,,5,3,6,4]--> [7,,5,3,6,4]
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n <= 1:
return 0
res = 0 small = prices[0]
for i in range(n):
if prices[i] < small:
small = prices[i] # 最小值
res = max(res, prices[i]-small)
return res
时间复杂度: O(n)
空间复杂度: O(1)
121. Best Time to Buy and Sell Stock@python的更多相关文章
- 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 ...
- 【刷题-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 ...
- 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 ...
- (Array)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 ----- java
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- C 语言实例 - 计算平均值
C 语言实例 - 计算平均值 C 语言实例 C 语言实例 使用数组来计算几个数的平均值. 实例 #include <stdio.h> int main() { int n, i; ], s ...
- 推荐一款功能齐全的开源客户端( iOS 、Android )研发助手。
DoraemonKit ,简称DoKit,中文名 哆啦A梦,意味着能够像哆啦A梦一样提供给他的主人各种各样的工具. 开发背景 每一个稍微有点规模的 App,总会自带一些线下的测试功能代码,比如环境切换 ...
- django-返回客户端外网ip服务
在服务器应用初始化的时候,比如salt-minion,需要在配置文件里说明自己的id,一般用ip. 如果都在一个内网里,从ip命令里获取就行了. 但现在的企业后台环境更加复杂,很多都是跨机房.有物理机 ...
- VMware虚拟机文件(后缀)详解
VMware虚拟机文件(后缀)详解 虚拟机的文件管理由VMware Workstation来执行,一个虚拟机一般以一系列文件的形式储存在宿主机中,这些文件一般在由workstation为虚拟机所创建的 ...
- [软件工程基础]2017.10.31 第四次 Scrum 会议
具体事项 项目交接燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #7 掌握 PHP:#6 阅读分析 PhyLab 数据处理相关代码 #10 搭建可用的开发测试环境: ...
- 【aspnetcore】让aspnetcore支持less文件
第一步:新建文件 CustomerFileExtensionContentTypeProvider namespace xxx { public class CustomerFileExtension ...
- 利用HtmlParser解析网页内容
一,htmpparser介绍 htmlparser是一个功能比较强大的网页解析工具,主要用于 html 网页的转换(Transformation) 以及网页内容的抽取 (Extraction). 二, ...
- Java基础教程(25)--I/O
一.I/O流 I/O流表示输入源或输出目标.流可以表示许多不同类型的源和目标,例如磁盘文件.设备.其他程序等. 流支持许多不同类型的数据,包括字节.原始数据类型.字符和对象等.有些流只传递数据 ...
- phpstorm类似sublime ctrl + alt +down多光标下移
http://blog.jetbrains.com/phpstorm/2014/03/working-with-multiple-selection-in-phpstorm-8-eap/ 评论有一条回 ...
- Docker快速构建Redis集群(cluster)
Docker快速构建Redis集群(cluster) 以所有redis实例运行在同一台宿主机上为例子 搭建步骤 redis集群目录清单 . ├── Dockerfile ├── make_master ...