zoukankan      html  css  js  c++  java
  • 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 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(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            a = 0
            for i in range(len(prices)-1):
                if prices[i]<prices[i+1]:
                    a += prices[i+1]-prices[i]
            return a

    画图很好理解。只要是斜率为正的直线都是收益,把这些记录下来就可以了。

  • 相关阅读:
    [Tensorflow2.0] 入门
    重新开始深度学习
    大秦小记三
    大秦小记二
    大秦小记一
    大秦小记初始篇
    js事件高级
    jsDOM
    css样式
    c#压缩文件和批量压缩文件
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/7610541.html
Copyright © 2011-2022 走看看