Skip to the code
cf.
+25 XP

#553 Trades With a Rest Day

medium

A trading simulator shows daily prices in prices. You may make as many trades as you like, but you can hold at most one share at a time, and after selling you must rest for one full day before buying again.

Write profitWithRest(prices) that returns the largest total profit.

Example 1

Input: prices = [2,3,4,1,3]
Output: 3

Buy at 2, sell at 3, rest on the day priced 4, buy at 1, sell at 3: 1 + 2.

Example 2

Input: prices = [5]
Output: 0

One day leaves no time to trade.

Constraints

  • 1 <= prices.length <= 5000
  • 0 <= prices[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).