[LC] 121. Best Time to Buy and Sell Stock
2024-09-07 07:41:25
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 1:
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.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if prices is None or len(prices) <= 1:
return 0
min_num, res = prices[0], -1
for num in prices:
if num < min_num:
min_num = num
if num - min_num > res:
res = num - min_num
return res
最新文章
- JQuery 概况
- java中常用的工具类(一)
- 状态机学习(三)解析JSON
- LoRaWAN协议(三)--Server端数据协议
- 函数(def)
- App跳转至系统Settings
- 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
- Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project
- AccountManager使用教程
- c++模板使用及实现模板声明定义的分离
- 监督学习——K邻近算法及数字识别实践
- centes7安装wdcp
- c语言,中缀表达式转后缀表达式并计算
- python令牌桶算法
- 分形之谢尔宾斯基(Sierpinski)地毯
- unity,荧光效果(bloom)实现过程
- 详解 Python3 正则表达式(五)
- Java常量字符串String理解 String理解
- unix 网络编程第八章 UDP
- Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件
热门文章
- 新浪sae url rewrite(伪静态、重定向)详解
- Freemarker的一点延生
- 干货 | 利用京东云Web应用防火墙实现Web入侵防护
- post表单、json接口
- Centos7下yum安装软件报错解决办法
- Codeforces Round #622 (Div. 2)C2 Skyscrapers最大";尖";性矩形,思维||分治
- 关于debug模式下对象toString报空指针的问题。Method threw &#39;java.lang.NullPointerException&#39; exception. Cannot evaluate cn.gooday.jsh.service.common.dto.RestControllerResult.toString()
- CodeForces 1287B Hyperset
- SQL的技巧
- 论文或github中一些通用思想