原题地址:https://oj.leetcode.com/problems/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 only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

解题思路:扫描一遍数组,使用low来标记最低价位,如果有更低的价位,置换掉。

代码:

class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
if len(prices) <= 1: return 0
low = prices[0]
maxprofit = 0
for i in range(len(prices)):
if prices[i] < low: low = prices[i]
maxprofit = max(maxprofit, prices[i] - low)
return maxprofit

最新文章

  1. Sql server 2008镜像配置步骤
  2. 【iOS】FMDB/SQLCipher数据库加解密,迁移
  3. NOI2012 : 迷失游乐园
  4. Helpers\Password
  5. javascript笔记6之函数
  6. VS2017 启动调试报错:ID为{....}进程未启动解决方案
  7. 亿级流量场景下,大型缓存架构设计实现【1】---redis篇
  8. 牛客练习赛43F Tachibana Kanade Loves Game
  9. django 防止xss攻击标记为安全的二种方法
  10. IdentityServer4【Topic】之登出
  11. 基于DBUtils实现数据库连接池
  12. maven编译的时候跳过test
  13. 事件的监听是由awt完成的
  14. VS中sln和suo的区别
  15. golang的json操作[转]
  16. java中线程同步问题
  17. 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
  18. PHP变量的传值和引用
  19. Navicat 12 连接 Mysql8.0 使用日志
  20. 分治FFT

热门文章

  1. Web服务器指纹识别工具httprint
  2. jQuery方法实现
  3. mysql where 条件中的字段有NULL值时的sql语句写法
  4. Intel P6以来核心架构及对应型号、芯片组一览表
  5. 【Codechef FRBSUM】【FJOI2016】【BZOJ4299】【BZOJ 4408】 可持久化线段树
  6. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
  7. How to tell if a file is an EXE or a DLL?
  8. Dapper Miser implementation of CMSIS-DAP, MC HCK as SWD Adapter
  9. C#访问远程主机资源的方法,多种方式
  10. [Unity3D]Unity3D游戏开发之角色控制漫谈