原题地址: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

[leetcode]Best Time to Buy and Sell Stock @ Python的更多相关文章

  1. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  2. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 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 al ...

  6. [LeetCode] 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 ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  9. [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

随机推荐

  1. Django 性能测试

    唐纳德·克努特(Donald Knuth)曾经说过:“不成熟的优化方案是万恶之源.”然而,任何一个承受高负载的成熟项目都不可避免地需要进行优化.在本文中,我想谈谈优化Web项目代码的五种常用方法.虽然 ...

  2. Linux驱动之混杂设备(misc)

    字符设备之混杂设备: 定义混杂设备: struct misdevice{ int minor; //为什么这里只有次设备号,因为混杂设备是一种在 /////////////////////////Li ...

  3. java 使用grpc步骤

    1.配置grpc maven依赖 <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-ne ...

  4. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  5. LOJ.2863.[IOI2018]组合动作(交互)

    题目链接 通过两次可以先确定首字母.然后还剩下\(n-1\)位,之后每一位只有三种可能. 最简单的方法是每次确定一位,通过两次询问显然可以确定.但是只能一次询问. 首字母只会出现一次,即我们可以将串分 ...

  6. hdu 5726 tetrahedron 立体几何

    tetrahedron/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Given four p ...

  7. GIT(7)----强制用远程代码覆盖本地修改

    清除本地修改 git reset --hard 拉代码 git pull Git Pull While Ignoring Local Changes? git pull 并强制覆盖本地修改

  8. spring cloud 学习(7) - 生产环境如何不停机热发布?

    业务繁忙的系统,原则上是不允许停机的,那么问题来了,如果真有严重的bug要修复,不得不发布,怎么做到不停机发布,对业务无感知呢? eureka 提供了一系列rest url,可以对注册实例进行操作,比 ...

  9. Wingdings 2 符号编码对照表

     http://blog.csdn.net/linux7985/article/details/5030754 符号  编码 HTML 代码  符号  编码 HTML 代码 ! ! " &q ...

  10. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...