Streaming Services
InterviewTime limit2sMemory limit512 MB
Given the daily release days for two streaming services, find the fewest K-day subscriptions needed so every day has a subscribed service releasing an episode.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Last year, you finally became a cord-cutter and cancelled your TV subscription to avoid the annoying ads. This was a relief. Suddenly, you could watch TV whenever you wanted, rather than when the channels decided. This happiness did not last long. The streaming services quickly adopted the old "one episode a week" model that TV channels used, and lunch conversations at work began to revolve around the episodes released the previous day.
A streaming service works like this. On certain days, a service releases exactly one new episode of a TV show. Every day, you need to watch an episode released that day so you can join the lunch conversations the next day.
Since your favourite streaming service may skip some days, you plan to use two streaming services instead. On any given day, you can buy a subscription to a service you do not currently subscribe to. A subscription lasts days, and during each of those days, you can watch episodes on the service you just subscribed to. Even if you already have an active subscription for a service, you may buy a subscription to the other service.
Given the dates when each service releases a new episode over the next days, determine the total number of subscriptions you need to buy so that every day you have an active subscription to a service that releases a new episode.
Input
The first line contains , the number of days the release schedule covers, and , the number of days each subscription lasts (). The next line is a string of characters, each of which is 1, 2, or B. The -th character tells you whether on day only the first service (1) releases a new episode, only the second service (2) does, or both (B) do.
Output
Output a single integer: the number of times you need to purchase a streaming service subscription so that you can watch a newly released episode each day.