Dodomeki
Sign in

131. Profit with a Day Off

Medium

You are given precos, where precos[i] is the price of a stock on day i. You may buy and sell as many times as you like, but you can hold at most one share at a time. After selling, you must wait one full day before buying again. Find the maximum possible profit; if no transaction yields a profit, return 0.

Example 1
Input: precos = [1, 2, 3, 0, 2]
Output: , 3
Explanation: One optimal sequence is to buy for 1, sell for 2, wait a day, buy for 0, and sell for 2.
Example 2
Input: precos = [2, 1, 4]
Output: , 3
Explanation: Buying for 1 and selling for 4 yields a profit of 3.
Example 3
Input: precos = [5, 4, 3, 2]
Output: , 0
Explanation: Since prices only decrease, it is best to make no transactions.
Constraints
  • 1 <= precos.length <= 10^5
  • 0 <= precos[i] <= 10^4
Dynamic programmingArrays
Caso 1
precos =
[1,2,3,0,2]
Expected =
3
Caso 2
precos =
[2,1,4]
Expected =
3
Caso 3
precos =
[5,4,3,2]
Expected =
0

Run checks these 3 cases. Submit runs all 12, including hidden ones.