我在面试蛋糕中解决了一个问题,并提出了通过单元测试的解决方案。
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):...
空间复杂度取决于输入。如果您输入的是一个列表,那么您已经将整个数据结构放入了内存中,即O(n)。如果您输入的是生成器,则min
/ max
可以一次简单地检查每个元素,而无需将所有元素都转储到内存中,即O(1)。