59: Best time to buy and sell stock

Given stock prices, write a method to maximize profit. You can buy and sell only once

Note: it's not allowed to sell before buy

 

Example 1

Input:   [5, 3, 6, 1, 9, 2, 15]

Output:  14

Explanation: Buy = 1, Sell = 15, Profit = 15 - 1

Example 2

Input:   [12, 6, 5, 3, 1]

Output:  0
Difficulty:Easy
Topic:Dynamic programming
Problem #:59