Maximize total reward by scheduling unit-time tasks before their deadlines

Quick Overview

Schedule unit-time tasks, each with a deadline and a reward, into numbered time slots so the total reward of the tasks finished on time is as large as possible. It tests greedy reasoning backed by an exchange argument and a fast way to find the latest free slot before a deadline.

Maximize total reward by scheduling unit-time tasks before their deadlines

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of `tasks` where each task is `[id, deadline, reward]`. Each task takes exactly one unit of time, and a task counts only if it is completed on or before its deadline. Time slots are numbered starting from 1, and only one task can run in any slot. Return the maximum total reward you can collect by choosing which tasks to run and when. ### Function Signature ```python def max_total_reward(tasks: list[list[int]]) -> int: ``` ### Rules - A task with deadline `d` can run in any free slot from 1 to `d`, inclusive. - Each task runs at most once, and tasks you do not schedule earn nothing. - The `id` only labels a task. The answer is the total reward, a single integer. ### Constraints - `1 <= len(tasks) <= 10^5` - `1 <= deadline <= len(tasks)` - `1 <= reward <= 10^4` ### Examples ```text Input: tasks = [[1,4,20],[2,1,10],[3,1,40],[4,1,30]] Output: 60 ``` Tasks 2, 3 and 4 all have deadline 1, so only one of them can run, and task 3 (reward 40) is the best. Task 1 (reward 20) goes in any later slot, such as slot 4, for a total of 40 + 20 = 60. ```text Input: tasks = [[1,2,100],[2,1,19],[3,2,27],[4,1,25],[5,1,15]] Output: 127 ``` Only slots 1 and 2 are usable. Task 1 (reward 100) runs in slot 2 and task 3 (reward 27) runs in slot 1, which beats pairing task 1 with any deadline-1 task (at most 100 + 25 = 125). ```text Input: tasks = [[1,3,50],[2,1,40],[3,2,35],[4,2,5]] Output: 125 ``` Task 1 runs in slot 3, task 2 in slot 1 and task 3 in slot 2, for 50 + 40 + 35 = 125. There are only three slots, so the task with reward 5 is the one left out.

Overview: Schedule unit-time tasks, each with a deadline and a reward, into numbered time slots so the total reward of the tasks finished on time is as large as possible. It tests greedy reasoning backed by an exchange argument and a fast way to find the latest free slot before a deadline.

Waymo logo
Waymo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of tasks where each task is [id, deadline, reward]. Each task takes exactly one unit of time, and a task counts only if it is completed on or before its deadline. Time slots are numbered starting from 1, and only one task can run in any slot.

Return the maximum total reward you can collect by choosing which tasks to run and when.

Function Signature

def max_total_reward(tasks: list[list[int]]) -> int:

Rules

  • A task with deadline d can run in any free slot from 1 to d , inclusive.
  • Each task runs at most once, and tasks you do not schedule earn nothing.
  • The id only labels a task. The answer is the total reward, a single integer.

Constraints

  • 1 <= len(tasks) <= 10^5
  • 1 <= deadline <= len(tasks)
  • 1 <= reward <= 10^4

Examples

Input: tasks = [[1,4,20],[2,1,10],[3,1,40],[4,1,30]]
Output: 60

Tasks 2, 3 and 4 all have deadline 1, so only one of them can run, and task 3 (reward 40) is the best. Task 1 (reward 20) goes in any later slot, such as slot 4, for a total of 40 + 20 = 60.

Input: tasks = [[1,2,100],[2,1,19],[3,2,27],[4,1,25],[5,1,15]]
Output: 127

Only slots 1 and 2 are usable. Task 1 (reward 100) runs in slot 2 and task 3 (reward 27) runs in slot 1, which beats pairing task 1 with any deadline-1 task (at most 100 + 25 = 125).

Input: tasks = [[1,3,50],[2,1,40],[3,2,35],[4,2,5]]
Output: 125

Task 1 runs in slot 3, task 2 in slot 1 and task 3 in slot 2, for 50 + 40 + 35 = 125. There are only three slots, so the task with reward 5 is the one left out.