python3.6中min()的空间复杂度是什么?>

问题描述 投票:0回答:1

我在面试蛋糕中解决了一个问题,并提出了通过单元测试的解决方案。

def get_max_profit(stock_prices):
    # Calculate the max profit
    if(len(stock_prices) == 0):
        raise ValueError('Empty stock')
    elif(len(stock_prices) == 1):
        return stock_prices[1]

    minValue = min(stock_prices)
    index_min = stock_prices.index(minValue)
    maxValue = max(stock_prices)
    index_max = stock_prices.index(maxValue)

    if(minValue == maxValue):
        return 0
    elif(index_max < index_min):
        return (stock_prices[1]-stock_prices[0])

    return (maxValue-minValue)

所以我在代码中使用了min,max函数。那么,使用列表中的min,max等内置函数的空间复杂度是多少?他们如何内部工作

我在面试蛋糕中解决了一个问题,并提出了通过单元测试的解决方案。 def get_max_profit(stock_prices):#计算最大利润if(len(stock_prices)== 0):...

python time-complexity space-complexity
1个回答
0
投票

空间复杂度取决于输入。如果您输入的是一个列表,那么您已经将整个数据结构放入了内存中,即O(n)。如果您输入的是生成器,则min / max可以一次简单地检查每个元素,而无需将所有元素都转储到内存中,即O(1)。

© www.soinside.com 2019 - 2024. All rights reserved.