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.lang.IllegalArgumentException: Document base F:\personal\projects\annoMVC\web does not exist or is not a readable directory
tomcat明明在d盘,怎么到F盘了...纳闷啊... 解决办法: 1停掉Tomcat服务2把/conf/Catalina/localhost下的文件删除掉(我在这个文件夹里发现了一个xml,指向了F ...
- HttpURLConnection(http 1.1) 用法、状态码、状态描述
最近研究了java的HttpURLConnection的用法, 这里简单的做一下记录: Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET ...
- LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...
- js实现排序去重计算字符次数
/*去重*/ var arr=[1,4,4,7,3,9,0,3,2,1,"你好","你","你好","你 "]; var ...
- tomcat下的公共jar包配置
1.创建my-lib文件夹 . 在conf文件中的catalina.properties中配置如下 # Licensed to the Apache Software Foundation (ASF) ...
- PHP:implode(),emplode() 字符串数组,数组字符串转换函数
1.implode()-Join array elements with a string(把数组元素组合为一个字符串.) string implode([string $separator,] ar ...
- Linux命令技巧:如何在Linux下重命名多个文件
我知道我可以用mv命令重命名文件.但是当我想重命名很多文件怎么办?如果为每个文件都这么做将会是很乏味的.有没有办法一次性重命名多个文件? 在Linux中,当你想要改变一个文件名,使用mv命令就好了.然 ...
- Uploadify 3.2 参数属性、事件、方法函数详解以及配置
一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...
- react树状组件
最近在react项目中需要一个树状组件,但是又不想因为这个去引入一套UI组件,故自己封装了一个基于react的树状组件, 个人认为比较难得部分在于数据的处理,话不多说直接上代码: 下面是tree.js ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 Apache 服务
想让我们的阿里云服务器成为一台 Web 服务器,我们需要安装一个 Web 服务器软件,比如 Apache ,或者 Nginx 等等.下面我们就一起来安装一个 Apache 服务. 我们可以使用 yum ...