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

最新文章

  1. JQuery 概况
  2. java中常用的工具类(一)
  3. 状态机学习(三)解析JSON
  4. LoRaWAN协议(三)--Server端数据协议
  5. 函数(def)
  6. App跳转至系统Settings
  7. 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
  8. Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project
  9. AccountManager使用教程
  10. c++模板使用及实现模板声明定义的分离
  11. 监督学习——K邻近算法及数字识别实践
  12. centes7安装wdcp
  13. c语言,中缀表达式转后缀表达式并计算
  14. python令牌桶算法
  15. 分形之谢尔宾斯基(Sierpinski)地毯
  16. unity,荧光效果(bloom)实现过程
  17. 详解 Python3 正则表达式(五)
  18. Java常量字符串String理解 String理解
  19. unix 网络编程第八章 UDP
  20. Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件

热门文章

  1. 新浪sae url rewrite(伪静态、重定向)详解
  2. Freemarker的一点延生
  3. 干货 | 利用京东云Web应用防火墙实现Web入侵防护
  4. post表单、json接口
  5. Centos7下yum安装软件报错解决办法
  6. Codeforces Round #622 (Div. 2)C2 Skyscrapers最大&quot;尖&quot;性矩形,思维||分治
  7. 关于debug模式下对象toString报空指针的问题。Method threw &#39;java.lang.NullPointerException&#39; exception. Cannot evaluate cn.gooday.jsh.service.common.dto.RestControllerResult.toString()
  8. CodeForces 1287B Hyperset
  9. SQL的技巧
  10. 论文或github中一些通用思想