题目来源:

  https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/


题意分析:

  和上题类似,给定array,代表第i天物品i的价格。如果可以交易无数次(手上有物品不能买),问最高利润。


题目思路:

  记录当前最小值,如果遇到array[i]  < min,那么加上当前的最大值;更新min。


代码(python):

  

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
ans,mins = 0,prices[0]
for i in prices:
if i > mins:
ans += i - mins
mins = i
return ans

[LeetCode]题解(python):122-Best Time to Buy and Sell Stock II的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 关于json文本数据的一些使用方法

    1.对象的存取 如果是对象的存取,可能需要序列化和反序列化对象的属性. NSDictionary params = @{@"hello":@"world"}; ...

  2. Ubuntu下屏幕录像、后期处理不完全攻略

    提要 如果要做成果展示或者效果演示,通常需要录取屏幕生成视频文件,在windows中我们可以用屏幕录像专家在录像, vegas 来做后期处理,Ubuntu可以么? 答案时当然可以!虽然第一次用觉得有点 ...

  3. HDU 4937 Lucky Number 规律题_(:зゝ∠)_

    把全部合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> type ...

  4. C语言的复合文字

    假设需要向一个带有int型参量的函数传递一个值,这时可以传递一个int型常量,也可以传递一个int型的变量.在C99标准之前,数组参数情况于现在不一样,没有所谓的数组常量可供传递,而在C99中增加了复 ...

  5. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  6. JavaScript引用类型之Array数组的拼接方法-concat()和截取方法-slice()

    1.concat()   基于当前数组中的所有项创建一个新数组(也就是副本),然后将接收到的参数添加到副本的末尾,最后返回新构建的数组.也就是说,concat()在向数组中追加元素时,不会改变原有数组 ...

  7. idea15破解

    注册方法:   注册码可以沿用14的,只是在 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK 14的话,网上可以找到一个,根据你的用户名 ...

  8. SDOI2008 Sandy的卡片( 后缀数组 )

    求出后缀数组, 然后二分答案, 对height数组分组检验答案. 时间复杂度O(|S| log|S|) ------------------------------------------------ ...

  9. python 实现单链表

    #! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures ...

  10. Linux命令之切换用户

    一.从 user 用户切换到 root 用户 不管是用图形模式登录 Ubuntu,还是命令行模式登录,我们会发现缺省的用户是 user,但是当我们需要执行一些具有 root 权限的操作(如修还系统文件 ...