“”"
股票买卖,给定一个数组,第i个元素代表第i天的股价。假设最多允许进行1次买卖,求可能的最大利润是多少?
- 示例: 输入price = [12, 15, 14, 8, 11, 10, 12], 则输出最大利润是4。
“”"
def get_max_profit(price):if price is None or len(price)==0:return 0min_num=price[0]max_profit=0for i in price:min_num=min(min_num,i)max_profit=max(max_profit,i-min_num)return max_profit# 函数调用格式如下
def main():price = [10,15,14, 6, 11, 10, 12]result = get_max_profit(price)print(result)if __name__ == '__main__':main()