题目来源:

  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. 智能家居项目(2):项目project框架的搭建

    项目管理器: Linux中的项目管理器"make"有些类似于windows中的Visual C++里的"project",它是一种控制编译或者反复编译软件的工具 ...

  2. javaScript 工作必知(二) null 和undefined

    null null 表示个“空” , 使用typeof (null) ;//Object ; 说明他是一个特殊的对象. null 类型只自己唯一个成员.他是不包含属性和方法的. undefined u ...

  3. grivid中切换按钮,两个按钮交替

    给grivdView不要设值 button的url和 commandName 在rowDatabound中操作变换 protected void GVData_RowDataBound(object ...

  4. javascript中数据类型转换

    转换为数字: parseInt():转换为整数型数值:从下标0开始判断,若为数值型则继续直到遇到非数值,返回前面的整数值: 小数点无效,若0开始为非数值则返回NaN: 转换空字符串会返回NaN: 能转 ...

  5. SQL Server 2012学习笔记 2 Server Core中命令行安装SQL

    Setup.exe /qs /ACTION=Install /FEATURES=SQLEngine,Replication /INSTANCENAME=MSSQLSERVER /SQLSVCACCOU ...

  6. 玩转kindle paperwhite: 如何越狱,安装强大外挂软件koreader

    NOTICE 1: 在更新kpvbooklet和使用最新版本的koreader(v2013.03-211)时候,会出现pdf文档无法重排的错误.亲测. 如果你是使用的最新版本koreader且出现上述 ...

  7. $.ajax传递字符串到后台,后台返回json对象

    var mall = { MallID: $("#createId").val().trim(), MallName: $("#createName").val ...

  8. 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程

    想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...

  9. 第一个Hadoop程序——Hello Hadoop

    本人原创,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/12773163 下载Hadoop程序包,下载地址:http://hadoop ...

  10. LineCalc,一个基于Lex&Yacc的简单行计算工具

    LineCalc是基于Lex&Yacc的一个简单的行计算工具,支持常见的运算符和部分POSIX中定义于math.h中的数学函数:同时,LineCalc还提供了一个简单的错误处理模块,能检测公式 ...