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的更多相关文章

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

  2. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  4. 【刷题-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 ...

  5. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

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

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

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

随机推荐

  1. UVaLive 7456 Least Crucial Node (并查集+暴力 或者 求割点)

    题意:求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多). 析:由于不知道要去掉哪个结点,又因为只有100个结点,所以我们考虑用一个暴力,把所有的结点都去一次,然后用并查集去判 ...

  2. XTU1266:Parentheses(贪心+优先队列)

    传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...

  3. hdoj3665【简单DFS】

    题意: 略. 思路: n就10而已,没有环,搜一下就好了.. #include <bits/stdc++.h> using namespace std; typedef long long ...

  4. 51nod1126【矩阵快速幂】

    思路: 自己的一点心得:中间矩阵为最终矩阵. 搞出来很简单的: #include <bits/stdc++.h> using namespace std; const int N=1e2+ ...

  5. hdoj1024【DP.最 大 m 字 段 和】(写完我都怕。。。不忍直视。。)

    弱弱上路,看了好多题解....[A的] 题意就是求最大m子段和. 我们先用a[1e6+7]存入数据: 定义:DP[ i , j ] 为前 j 个元素的 i 个子段的最大和,且第 i 个子段中包含了元素 ...

  6. python __builtins__ range类 (56)

    56.'range',  创建一个整数列表 class range(object) | range(stop) -> range object | range(start, stop[, ste ...

  7. Codeforces731E Funny Game

    dp[i][0]表示从i出发,轮到先手走的最优值. dp[i][1]表示从i出发,轮到后手走的最优值. dp[i][0]=max(dp[j][1]+sum[j]) dp[i][1]=min(dp[j] ...

  8. [WOJ1583]向右看齐

    题目链接: WOJ1583 题目分析: 大水题--我就来水个题解 倒序扫,单调栈维护单减序列,每个对象的答案是栈里它下面那个元素 代码: #include<bits/stdc++.h> # ...

  9. [WOJ1138]最大子序和

    题目链接: WOJ1138 题目分析: 是很经典的一道题,乱搞的方法应该有不少,这里介绍O(n)的单调队列做法 首先维护一个前缀和,然后枚举每一个位置,维护一个前缀和单增的单调队列,但队列仅储存下标, ...

  10. 【Helvetic Coding Contest 2018】B2. Maximum Control (medium)

    Description 传送门(翻译就别想了,本人英语太垃圾) Solution 设ans[i]为设置i个船时能控制的最多星球数(看到这你可能因为是dp,然而我可以很负责地告诉你是假的) 首先一个显然 ...