原题地址:https://oj.leetcode.com/problems/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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解题思路:由于可以进行无限次的交易,那么只要是递增序列,就可以进行利润的累加。

代码:

class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
maxprofit = 0
for i in range(1, len(prices)):
if prices[i] >= prices[i-1]:
maxprofit += prices[i] - prices[i-1]
return maxprofit

[leetcode]Best Time to Buy and Sell Stock II @ Python的更多相关文章

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

  2. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  3. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

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

  6. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  7. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  8. [LeetCode] 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 ...

  9. LeetCode: Best Time to Buy and Sell Stock II [122]

    [题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...

随机推荐

  1. Django模版语言inclusion_tag的用法。

        inclusion_tag.它多用于一个HTML片段的.例如我写的一个BBS项目中. 一个博主的主页面的左侧栏和查看博主某篇文章的页面的左栅栏的一样的.为了不用重复写同样的代码.且提高页面的扩 ...

  2. openQPA[01]初次认识与使用

    开源项目QPA 1.项目主页:[http://protocol.sinaapp.com/] 2.项目介绍: 3.运行项目: (1)安装python2.7,并安装PyQt4.   下载地址[https: ...

  3. tarjan算法讲解

    tarjan算法,一个关于 图的联通性的神奇算法.基于DFS算法,深度优先搜索一张有向图.!注意!是有向图.根据树,堆栈,打标记等种种神奇方法来完成剖析一个图的工作.而图的联通性,就是任督二脉通不通. ...

  4. Loj10154 选课

    试题描述: 大学实行学分制.每门课程都有一定的学分,学生只要选修了这门课并通过考核就能获得相应学分.学生最后的学分是他选修各门课的学分总和.每个学生都要选择规定数量的课程.其中有些课程可以直接选修,有 ...

  5. JVM启动流程

    JVM启动流程 (1)在java中jvm是通过java或javaw命令启动的,后面跟加载的类名. (2)jvm在启动的时候先根据[当前路径和系统版本寻找jvm的配置文件jvm.cfg]装载配置. (3 ...

  6. STM32 TIMER REGISTER

  7. Parallel Programming--perfbook

    https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html

  8. Pylons安装苦逼之路

    本文介绍一下我在安装pylons的过程中出现的一些错误和解决办法,当然这些都是不完全版. 1.在Serve1(服务器Python版本2.4.3)上面装环境的时候总是出现with_statement有关 ...

  9. Android adb logcat使用技巧

    前言 新买的笔记本E431装了最新版的Eclipse,搞定了Android开发环境,可是logcat里查看东西居然仅仅显示level,没有错误的具体信息.我本身也不是一个愿意折腾图形界面,更喜欢纯命令 ...

  10. delphi 结构体和TList的用法

    type  PRecord = ^TMyRec;  TMyRec = record    s: string[8];    i: integer;    d: double;end;var   MyL ...