Data Structure & Algorithm: Level-1 Problem #22. Maximum Profit

Given a list of prices for an item, find out the maximum profit to buy in first at a low price and sell it later at a high one.

Sample Input:

[ 4, 1, 5, 3, 7, 6 ]

Sample Output:

6

The maximum profit is 6 to buy in at price 1 and sell out at price 7.


数据结构和算法:初级练习题 #22 – 最大利润


给定一个物品的一组价格,以尽可能的低价买入然后以尽可能的高价卖出,以获取最大利润。计算买卖该物品的最大利润是多少。

输入样例:

[ 4, 1, 5, 3, 7, 6 ]

输出样例:

6

输入样例中的最大利润是6,以价格 1 买入,以价格 7 卖出。


Python Solution

Maximum Profit