The maximum profit shares: to prove safety Offer (Python variety of ideas to achieve)
63 interview questions
Topic: The biggest profit shares
Question: Suppose the price of a share in chronological order stored in an array, what's largest trading profits once the stock available is how much? For example, a stock price for some time node 9,11,8,5,7,12,16,14 {}.
If we can at the price of 5 when buying and selling price is 16, is able to get the maximum profit is 11.
Problem-solving ideas:
class Solution():
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
min_price=float('inf')
max_profit=0
for price in prices:
if price<min_price:
min_price=price
profit=price-min_price
max_profit=max(max_profit,profit)
return max_profit