1175. Best Time to Buy and Sell Stock I

MediumDynamic ProgrammingStock DP

Given daily prices of a stock, you may complete at most one transaction (buy once and sell once, buying before selling). Return the maximum profit, or 0 if none is possible. The input is JSON {prices}.

Input: JSON {prices}.

Output: Integer — the maximum profit.

Examples

Example 1
Input: {"prices":[7,1,5,3,6,4]}
Output: 5
Explanation: Buy at 1, sell at 6.
Example 2
Input: {"prices":[7,6,4,3,1]}
Output: 0
Explanation: Prices only fall.
Example 3
Input: {"prices":[1]}
Output: 0
Explanation: No transaction possible.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →