[LeetCode&Python] Problem 122. 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 algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
ans=0
if prices:
n=len(prices)
i=0
valley=prices[0]
peak=prices
while i<n-1:
while i<n-1 and prices[i]>=prices[i+1]:
i+=1
valley=prices[i]
while i<n-1 and prices[i]<=prices[i+1]:
i+=1
peak=prices[i]
ans+=peak-valley
return ans
[LeetCode&Python] Problem 122. Best Time to Buy and Sell Stock II的更多相关文章
- LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
Description Say you have an array for which the ith element is the price of a given stock on day i. ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- python heapq模块使用
Python内置的heapq模块 Python3.4版本中heapq包含了几个有用的方法: heapq.heappush(heap,item):将item,推入heap >>> it ...
- ubuntu下的网速限制软件wondershaper (2011-09-18 00:00:00)转载▼
上网或下载的时候我们常常希望网速快一点,但有时我们也需要限制网速,在ubuntu系统下,可以使用wondershaper,不仅可以限制下载速度还可以限制上传速度. 安装好之后,需要使用终端取得管理员权 ...
- 逆袭之旅DAY24.XIA.二重进阶、双色球
一. 选择题. 1. 以下关于二重循环的说法正确的是(D). A. 二重循环就是一般程序中只能有两个循环 B. While循环不能嵌套在for循环里 C. 两个重叠的循环不能嵌套在第三个循环里. D. ...
- Ubuntu 16.04 中安装谷歌 Chrome 浏览器
http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...
- Python Django 之 Template 模板的使用
一.模板样式 注意: 1.url urlpatterns = { path('admin/', admin.site.urls), path('order/', views.order), path( ...
- JXL生成Excel,并提供下载(1:生成Excel)
public String exportExcel(long id) { String preeReviewName = "文件名"; String filePath = 路径名; ...
- 读书笔记 C# yield return与yield break执行顺序的浅析
yield return可一次返回一个元素,并保留当前在代码中的位置,下次调用当前迭代器函数时,将从该位置从新执行.也就是说执行了yield return的时候,迭代器函数就返回了一个元素给forea ...
- 小程序之setData特殊情况 三种情况的wx:if
比如data{ “a”:{}, "b":{} } 你想完成这样的结构 //创建一个对象 var readyData={} //对象[key] =另一个对象 readyData[ke ...
- box-shadow 边框阴影
box-shadow: 0 0 20px #000 inset;