题目

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

代码:oj测试通过 Runtime: 71 ms

 class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
# none case or one element case
if prices is None or len(prices)<2 :
return 0
# dp
buy = 0
sell = 0
profit = 0
for i in range(len(prices)-1):
if prices[i]<=prices[i+1]:
sell = i+1
else:
profit = profit + prices[sell]-prices[buy]
buy = i+1
sell = i+1
return profit+max(0,prices[sell]-prices[buy])

思路

采用贪心算法。

代码的逻辑还算清晰:找到最长的上升区间,最低卖最高买即可;然后再找下一个上升区间;退出循环的时候注意处理一下最后一个上升或下降区间。

for循环:

1. if的部分就是不断找更大的上升空间,找到了就一定把sell放在更利润的卖点。

2. else的部分处理的是价格下降的情况,buy和sell需要同时跟进,这样保证同样的买卖价格,做到不赔钱

有一个梗:代码AC后,我review时发现if和else里面都有sell=i+1这个语句,那么既然不管是if或else都得执行sell=i+1,为啥还要在每个语句里面单拎出来呢?于是就有了下面的代码(这部分代码是错误举例用的):结果竟然是报错。

            sell = i+1
if prices[i]>prices[i+1]:
profit = profit + prices[sell]-prices[buy]
buy = i+1

这是一个思维的陷阱:不错,确实在if和else里面都执行了sell=i+1这个语句,但是语句执行的逻辑顺序是不一样的。请注意,如果是prices[i]>prices[i+1]的条件下,sell=i+1是在profit语句之后执行的。于是恍然大悟,修改成如下的代码:

oj测试通过 Runtime: 67 ms

class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
# none case or one element case
if prices is None or len(prices)<2 :
return 0
# dp
buy = 0
sell = 0
profit = 0
for i in range(len(prices)-1):
if prices[i]>prices[i+1]:
profit = profit + prices[sell]-prices[buy]
buy = i+1
sell = i+1
return profit+max(0,prices[sell]-prices[buy])

这样代码的逻辑就更简洁了,可以解释如下:只要相邻两天,后一天比前一天高就可以低买高卖产生利润。

第二个更简洁的代码,似乎也是网上更多的人列出来的答案。简洁的解法也可以由一般的解法推演出来。

leetcode 【 Best Time to Buy and Sell Stock II 】python 实现的更多相关文章

  1. [leetcode]Best Time to Buy and Sell Stock II @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...

  2. 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 ...

  3. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  4. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  5. 122. Best Time to Buy and Sell Stock II@python

    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 II 买股票的最佳时间之二

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

  7. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

  8. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  9. [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 ...

  10. LeetCode: Best Time to Buy and Sell Stock II [122]

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

随机推荐

  1. <Android 基础(五)> MVVM

    介绍 MVVM,Model-View-ViewModel,与上次讲的MVP模式比较的类似,MVP中需要大量的接口文件,而MVVM模式下,View和ViewModel直接关联,使用上比较方便,简化了代码 ...

  2. Android接入支付宝支付实现

    接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...

  3. C#基础知识图谱

  4. Samuraiwtf-解决sshd安装启动

    东西没仔细看就给别人了,具体是系统默认的sshd怎么都起不来.于是重新安装一下,过程如下 root@samuraiwtf:~# netstat -nlt Active Internet connect ...

  5. 使用SAP云平台的destination消费Internet上的OData service

    通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...

  6. [VC]线程

    是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共 享进程所拥有的全部资源.一个线程可以创建和撤消另一个线 ...

  7. 2017.12.11 String 类中常用的方法

    1.编写程序将 "jdk" 全部变为大写,并输出到屏幕,截取子串"DK" 并输出到屏幕 package demo; import java.util.Scann ...

  8. C语言 流缓冲 Stream Buffering

    From : https://www.gnu.org/software/libc/manual/html_node/Stream-Buffering.html 译者:李秋豪 12.20 流缓冲 通常情 ...

  9. WARNING you have Transparen Huge Pages..

    redis启动警告: WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will c ...

  10. Python实现注册和登录

    一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...