• 122. Best Time to Buy and Sell Stock II


    Say you have an array prices 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 (i.e., buy one and sell one share of the stock multiple times).

    Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

    买股票问题,可以任意次数买卖,但是买的时候手上必须没股票了。做法很简单,只要判断今天的股价是否大于昨天的,大于的话就加上收益,因为你总可以昨天买今天卖然后今天买明天又卖。

    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            ans = 0
            for i in range(1, len(prices), 1):
                if prices[i] > prices[i - 1]:
                    ans += prices[i] - prices[i - 1]
            return ans
            
  • 相关阅读:
    PID 不能控制哪些系统?
    矩阵正定、负定、半正定、半负定
    KKT条件(不等式约束优化)
    腾讯OCR身份证正面识别
    小程序
    tomcat相关
    linux安装tomcat
    linux防火墙
    nginx操作
    Clean Code
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13226801.html
Copyright © 2020-2023  润新知