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 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.
原题地址: Best Time to Buy and Sell Stock II
难度: Easy
题意: 买卖股票,允许多次买卖,但不允许同一天即买入又卖出,返回最大利润
思路:
遍历数组, 计算当前一个数与前一个数的差值, 差值大于0,表明是利润
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
res = 0 for i in range(1, n):
profit = prices[i] - prices[i-1]
if profit > 0:
res += profit
return res
时间复杂度: O(n)
空间复杂度: O(1)
122. Best Time to Buy and Sell Stock II@python的更多相关文章
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
- !!!!!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 al ...
- 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 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时11
课时11 神经网络训练细节part1(下) 2010年,Glorot等人写的论文,我们称之为Xavier初始化,他们关注了神经元的方差表达式.他们推荐一种初始化方式,那就是对每个神经元的输入进行开根号 ...
- C#文件读写参考
string s = ""; string[] files = Directory.GetFiles(@"E:\u3d_warcraft\logic\logic\Batt ...
- git操作实战指南
1 背景 小白进入公司,进入日常多人开发,git的使用应该是新人要掌握的第一个技能.git是一个分布式数据存储库,分为远程存储和本地存储,本地存储的话,每一台计算机就相当于一个存储数据库,可以记录和存 ...
- 洛谷 - P1433 - 吃奶酪 - dfs
https://www.luogu.org/problemnew/show/P1433 并不是每一个求最短距离就是bfs,这个肯定是dfs. 直接计算15!可以知道枚举必定超时,但是! 我们dfs非常 ...
- IOS高级开发~Runtime(一)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...
- win10家庭版安装
https://www.microsoft.com/zh-cn/software-download/windows10ISO https://www.2cto.com/os/201704/621770 ...
- hdu 1028 Ignatius and the Princess III【生成函数】
老是想着化简,实际上O(n^3)就行了-- 写成生成函数是\( \prod_{i=1}^{n}(1+x^i+2^{2i}+...+x^{ \left \lfloor \frac{n}{i} \righ ...
- ExtWebComponents
我们很高兴地宣布Sencha ExtWebComponents的早期版本现已推出.ExtWebComponents提供了数百个预构建的UI组件,您可以轻松地将它们集成到使用任何框架构建的Web应用程序 ...
- P1251 递推专练3
递推专练3 描述 Description 圆周上有N个点.连接任意多条(可能是0条)不相交的弦(共用端点也算相交)共有多少种方案? 输入格式 Input Format 读入一个数N.<=N< ...
- bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】
题目传送门 Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruser i 银行的 ATM 取款机.令人奇怪的是,S ...