Best Time to Buy and Sell Stock

Easy
Arrays Array Dynamic Programming

Given array of stock prices, find max profit from one buy and one sell. Print 0 if no profit.

Constraints

1 ≤ prices.length ≤ 10^5

Examples

Example 1:
Input: 7 1 5 3 6 4
Output: 5
Explanation: Buy at 1, sell at 6
Example 2:
Input: 7 6 4 3 1
Output: 0
▲ Console

Install Talent Arabia

Get instant access to jobs and career tools on your device.