461. Cinema Hall Movie Schedule
Cinema Hall Movie Schedule

A cinema hall has one screen and operates from 10 AM to 11 PM each day. Given its movie catalog and existing screenings, determine whether one additional movie of the requested duration can fit without changing or removing any existing screening.

Method Signature

public boolean canSchedule(int durationInMinutes, List<String> movies, List<String> screenings)
  • durationInMinutes: Duration of the movie to be added.
  • movies: Each string is "title,duration", such as "Ocean Trail,180". Titles are unique and matched exactly, including letter case.
  • screenings: Each string is "title,startTime", such as "Ocean Trail,600". The title identifies a movie in the catalog, which supplies its duration. A movie may have multiple screenings.
  • Return true if the additional movie fits in at least one uninterrupted free interval; otherwise, return false. This method only checks availability and does not update the schedule.

Scheduling Rules

  • Times are whole minutes from midnight. Opening time is 600; closing time is 1,380.
  • A screening starting at startTime ends at startTime + duration. It must start at or after opening and finish at or before closing.
  • Screenings cannot overlap. One screening may start exactly when another ends; no additional break is required.
  • The new movie has no fixed start time. Free intervals before the first screening, between screenings, and after the last screening are eligible.

Constraints

  • 0 ≤ movies.size() ≤ 1,000
  • 0 ≤ screenings.size() ≤ 780
  • 1 ≤ durationInMinutes ≤ 1,000; catalog durations satisfy the same bounds.
  • Titles contain between 1 and 100 characters and contain no commas. All records are correctly formatted, and all lists and strings are provided.
  • Every screening title exists in movies. Existing screenings are non-overlapping and satisfy 600 ≤ startTime < endTime ≤ 1,380.
  • Both lists may be in any order. An empty screening list means the screen is free throughout opening hours.

Examples

Example 1: Fit Between Screenings

Input: canSchedule(durationInMinutes = 120, movies = ["Ocean Trail,180", "City Lights,300", "Moon Ride,140"], screenings = ["City Lights,1080", "Ocean Trail,600", "Moon Ride,900"])

Output: true

Explanation: Ocean Trail ends at minute 780, and Moon Ride starts at minute 900. The new movie fits exactly in this 120-minute gap.

Example 2: Separate Gaps Cannot Be Combined

Input: canSchedule(durationInMinutes = 121, movies = ["Ocean Trail,180", "City Lights,300", "Moon Ride,140"], screenings = ["City Lights,1080", "Ocean Trail,600", "Moon Ride,900"])

Output: false

Explanation: The free gaps last 120 and 40 minutes. Although 160 minutes are free in total, neither gap can hold a 121-minute movie.

Example 3: Empty Schedule

Input: canSchedule(durationInMinutes = 780, movies = [], screenings = [])

Output: true

Explanation: The movie can start at minute 600 and finish exactly at closing time, minute 1,380.

Input: canSchedule(durationInMinutes = 781, movies = [], screenings = [])

Output: false

Explanation: Only 780 minutes are available during opening hours.



Please use Laptop/Desktop or any other large screen to add/edit code.