Buy Stock I

2018-05-27  本文已影响0人  GakkiLove

Given an array of positive integers representing a stock’s price on each day. On each day you can only make one operation: either buy or sell one unit of stock and you can make at most 1 transaction. Determine the maximum profit you can make.

Assumptions

The given array is not null and is length of at least 2.

Examples

{2, 3, 2, 1, 4, 5}, the maximum profit you can make is 5 - 1 = 4

class Solution(object):
  def maxProfit(self, array):
    max_pro,min_pri = 0,float('inf')
    for price in array:
      min_pri = min(min_pri,price)
      profit = price - min_pri
      max_pro = max(max_pro,profit)
    return max_pro
上一篇 下一篇

猜你喜欢

热点阅读