题目来源:

  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. poj2186 Popular Cows --- 强连通

    给一个有向图,问有多少结点是其它全部结点都能够到达的. 等价于,在一个有向无环图上,找出度为0 的结点.假设出度为0的结点仅仅有一个,那么这个就是答案.假设大于1个.则答案是0. 这题有环.所以先缩点 ...

  2. Linux ln命令具体解释及使用

    Linux ln命令具体解释及使用 ln是linux中一个很重要命令,它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最经常使用的參数是-s,详细使用方法是:ln –s 源文件 目标文 ...

  3. css两个紧挨着的css选择器修饰同一个元素

    #status, .commands{ height: 25px; line-height: 25px;}.upload .commands{ float: right;}.hidden{ displ ...

  4. JavaScript之怎样获取元素节点

    JavaScript获取元素节点一共有三种方法,分别是通过元素ID.通过标签名字和通过类名字来获取: 1.通过元素ID属性的ID值来获得元素对象-getElementById() DOM提供了一个名为 ...

  5. C++数据类型简析

    C++语言的基本数据类型有如下四种: 整型,说明符为int: 字符型,说明符为char: 浮点型(又称实型),说明符为float(单精度),double(双精度): 空值型,说明符为void,用于函数 ...

  6. 交换机access和trunk的一些小结(转)

     以太网端口有 3种链路类型:access.trunk.hybird Access类型端口只能属于1个VLAN 般用于连接计算机 端口: Trunk类型端口可以允许多个VLAN通过,可以接收和发送多个 ...

  7. mysql不区分大小写解决

    今天遇到一个情况,前台验证用户昵称的时候发现无论输入Fred fred亦或是FrEd 都会显示昵称存在(这并不是我所期望的结果) debug发现并不是程序问题 hibernate也只是吧hql装成my ...

  8. 使用PHP把下划线分隔命名的字符串 转换成驼峰式命名方式 , 把下划线后面的第一个字母变成大写

    最近项目使用symfony框架,这个框架对数据库的操作在这个团队里使用的是ORM进行操作,说实话使用ORM的开发效率和运行效率不一定高多少,到是它的实体命名和现有数据库字段的命名不太一样,ORM实体属 ...

  9. Android 4.0 ProGuard 代码混淆 以及 proguard returned with error code 1.See console异常的解决方法

    最近呢说要上线,就去找了下上线的方法...之前做过代码混淆,用的是progarud.cfg,但是呢自己反编译了之后还是无效,然后就丢着先不管了,因为实在不知道什么情况.今天来上线的时候结果总是报错,总 ...

  10. QTreeView处理大量数据(使用1000万条数据,每次都只是部分刷新)

    如何使QTreeView快速显示1000万条数据,并且内存占用量少呢?这个问题困扰我很久,在网上找了好多相关资料,都没有找到合理的解决方案,今天在这里把我的解决方案提供给朋友们,供大家相互学习. 我开 ...