1176. Best Time to Buy and Sell Stock II — Unlimited

MediumDynamic ProgrammingStock DP

Given daily stock prices, you may complete as many transactions as you like (buy then sell, holding at most one share at a time). Return the maximum total profit. The input is JSON {prices}.

Input: JSON {prices}.

Output: Integer — the maximum total profit.

Examples

Example 1
Input: {"prices":[7,1,5,3,6,4]}
Output: 7
Explanation: Profit 4 then 3.
Example 2
Input: {"prices":[1,2,3,4,5]}
Output: 4
Explanation: Buy day 1, sell day 5.
Example 3
Input: {"prices":[5]}
Output: 0
Explanation: No transaction.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →