Code Logo

Harbor Banner Schedule Slots

Published at05 Jan 2026
Hard 2 views
Like12

You are given the travel days that need coverage and the prices of different pass lengths. The goal is to cover every required day with the minimum total cost.

A pass does not have to start at day 1. You buy it on a specific travel day, and it covers a block of future days depending on its duration. That means sometimes it is cheaper to buy one longer pass, and sometimes several shorter passes work better.

For example, if days = [1,2,3,4,5,6,7] and costs = [3,8,20], the answer is 9 because three 3-day passes are cheaper than other combinations. If there are no travel days, the answer is 0 because no pass is needed at all.

So the task is to choose passes that cover all required days while keeping the total cost as low as possible.

Example Input & Output

Example 1
Input
days = [1,2,3,4,5,6,7], costs = [3,8,20]
Output
9
Explanation

Three 3-day passes cover the entire week for cost 9.

Example 2
Input
days = [], costs = [5,10,20]
Output
0
Explanation

No scheduled nights mean no passes are needed.

Example 3
Input
days = [1,4,6,7,8,20], costs = [2,7,15]
Output
11
Explanation

Buy 1-day passes for nights 1,4,6 and a 7-day pass covering 7,8,20.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Banner Schedule Slots
Recommendation Algorithm Flow for Harbor Banner Schedule Slots

Solution Approach

This is a dynamic programming problem over the travel-day list. Let dp[i] be the minimum cost needed to cover travel days starting from index i.

At each travel day, you try buying each available pass type. A pass covers the current day and also skips forward past every future travel day that falls inside its coverage window.

So the transition looks like this idea:

dp[i] = min(
  cost1 + dp[nextIndexAfterPass1],
  cost2 + dp[nextIndexAfterPass2],
  cost3 + dp[nextIndexAfterPass3]
)

The base case is that once all travel days are covered, the remaining cost is 0.

You can implement this with memoized recursion or bottom-up DP. The key is that every decision at day i only depends on which pass you buy there and which travel day is the first one not covered by that pass.

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_min_slots(int[][] intervals) {
        int[] starts = new int[intervals.length];
        int[] ends = new int[intervals.length];
        for (int i = 0; i < intervals.length; i++) {
            starts[i] = intervals[i][0];
            ends[i] = intervals[i][1];
        }
        Arrays.sort(starts);
        Arrays.sort(ends);
        int slots = 0, pEnd = 0;
        for (int s : starts) {
            if (s < ends[pEnd]) slots++;
            else pEnd++;
        }
        return slots;
    }
}