Maximize the number of shows watched within a time limit

Quick Overview

Find the largest number of shows that can be watched in full within a total time budget, given each show's duration. The problem tests a greedy choice backed by an exchange argument, sorting, and the boundary case where the total exactly equals the limit.

Maximize the number of shows watched within a time limit

Company: Bill.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `durations` where `durations[i]` is the duration of the i-th show, and an integer `time_limit` representing the total time available, return the maximum number of shows you can watch without exceeding the time limit. You may watch shows in any order, but each show must be watched in full. ### Function Signature ```python def max_shows(durations: List[int], time_limit: int) -> int: ``` ### Rules - Each entry of `durations` is a separate show and can be watched at most once. - A total watching time equal to `time_limit` does not exceed the limit, so it is allowed. ### Constraints - `1 <= durations.length <= 10^5` - `1 <= durations[i] <= 10^4` - `0 <= time_limit <= 10^9` ### Examples ```text Input: durations = [30,60,45,20,90], time_limit = 100 Output: 3 ``` Sorted, the durations are [20, 30, 45, 60, 90]. The shows of length 20, 30 and 45 sum to 95, which fits within 100. Any four shows take at least 20 + 30 + 45 + 60 = 155, so four is impossible. ```text Input: durations = [50,50,50], time_limit = 40 Output: 0 ``` Every show is longer than the time limit, so no show can be watched. ```text Input: durations = [25,10,40,10], time_limit = 45 Output: 3 ``` The shows of length 10, 10 and 25 sum to exactly 45, which is allowed. Adding the show of length 40 would bring the total to 85.

Overview: Find the largest number of shows that can be watched in full within a total time budget, given each show's duration. The problem tests a greedy choice backed by an exchange argument, sorting, and the boundary case where the total exactly equals the limit.

Bill.com logo
Bill.com
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array durations where durations[i] is the duration of the i-th show, and an integer time_limit representing the total time available, return the maximum number of shows you can watch without exceeding the time limit.

You may watch shows in any order, but each show must be watched in full.

Function Signature

def max_shows(durations: List[int], time_limit: int) -> int:

Rules

  • Each entry of durations is a separate show and can be watched at most once.
  • A total watching time equal to time_limit does not exceed the limit, so it is allowed.

Constraints

  • 1 <= durations.length <= 10^5
  • 1 <= durations[i] <= 10^4
  • 0 <= time_limit <= 10^9

Examples

Input: durations = [30,60,45,20,90], time_limit = 100
Output: 3

Sorted, the durations are [20, 30, 45, 60, 90]. The shows of length 20, 30 and 45 sum to 95, which fits within 100. Any four shows take at least 20 + 30 + 45 + 60 = 155, so four is impossible.

Input: durations = [50,50,50], time_limit = 40
Output: 0

Every show is longer than the time limit, so no show can be watched.

Input: durations = [25,10,40,10], time_limit = 45
Output: 3

The shows of length 10, 10 and 25 sum to exactly 45, which is allowed. Adding the show of length 40 would bring the total to 85.