Skip to the code
cf.
+25 XP

#500 Cheapest API Plan for Busy Days

medium

Your app calls a paid map API only on some days of the year, listed in days in increasing order (days are numbered 1 to 365). The provider sells three passes: 1 day for prices[0], 7 consecutive days for prices[1], and 30 consecutive days for prices[2]. A pass bought on day d covers days d to d + 6 (or d + 29).

Write cheapestPlan(days, prices) that returns the least total cost to cover every day in days.

Example 1

Input: days = [2,5,7,8,9,21], prices = [2,7,15]
Output: 11

A 1-day pass on day 2 (2), a 7-day pass covering days 5 to 11 (7), and a 1-day pass on day 21 (2).

Example 2

Input: days = [2,3,4,5,6,7,8,9,10,11,31,32], prices = [2,7,15]
Output: 17

A 30-day pass for days 2 to 31 (15) and a 1-day pass on day 32 (2).

Constraints

  • 1 <= days.length <= 365
  • 1 <= days[i] <= 365, strictly increasing
  • prices.length === 3, 1 <= 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).