leetcode 【 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 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 实现的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- Java开发工具IntelliJ IDEA创建Andriod项目示例说明
IntelliJ IDEA社区版作为一个轻量级的Java开发IDE,是一个开箱即用的Android开发工具. 注意:在本次的教程中我们将以Android平台2.2为例进行IntelliJ IDEA的使 ...
- SpringBoot的优缺点
优点: 1.快速构建项目 2.对主流开发框架的无配置继承 3.项目可独立运行,无须外部依赖Servlet容器 4.提高运行时的应用监控 5.极大地提高了开发.部署效率 6.与云计算的天然集成 缺点: ...
- LEMP (LNMP) Stack-5.4.16 (OpenLogic CentOS 7.2)
LEMP (LNMP) Stack-5.4.16 (OpenLogic CentOS 7.2) 平台: CentOS 类型: 虚拟机镜像 软件包: mariadb-5.5.47 nginx-1.6.3 ...
- PHP snippets
Friendly file size string public static function bytesToSize($bytes) { if ($bytes < 1024) { retur ...
- Win10 耳机无声音,扬声器有声音的解决办法
注:适用于WIN10及WIN10.ubuntu双系统耳机无声音的问题. 在WIN10的引导下,安装了Ubuntu的桌面版,作成了双系统,可是发现了一个问题:进入Win10后插耳机会没有声音,扬声器有声 ...
- 实战:ADFS3.0单点登录系列-前置准备
本文为本系列第二篇,主要分为两部分进行介绍, 一.网络拓扑 二.证书制作 还是将本系列目录贴出来,方便导航 实战:ADFS3.0单点登录系列-总览 实战:ADFS3.0单点登录系列-前置准备 实战:A ...
- iOS解析新浪微博的@##以及URL链接并展示
最近在做一个跟微博相关的应用.其中涉及到了对微博中@.##以及URL链接的解析与展示.分享一下个人处理的方式,希望对需要的人有所帮助. 最终的展现效果: 首先,第一步是你得从纯文本中找到它们.毫无疑问 ...
- POJ 3057 Evacuation(二分匹配)
分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...
- 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现, 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog ...
- python_8_guess
#python3和2都可以 #方法1 age_of_oldboy=56 count=0 while True: if count==3: break guess_age=int(input('gues ...