KatInterview

Maximize Priority Sum of Non-Overlapping Tasks

Dynamic ProgrammingAuto-graded · 18 test cases
HardAuto-gradedDynamic Programmingdynamic-programminggreedy-intervalsbinary-search

Last updated

Find the maximum priority total achievable among non-overlapping interval tasks, prioritizing task count first then total priority.

You are analyzing execution logs from a task processing engine. Each task is represented as a tuple (start_time, end_time, priority).

You must select a subset of tasks to execute such that:

  1. No two chosen tasks overlap in time. Two tasks are allowed to share an endpoint (for example, a task ending at time 3 and another starting at time 3 do not overlap).
  2. The number of selected tasks is maximized.
  3. If there are multiple valid subsets that achieve the maximum count of tasks, the subset with the highest total priority is preferred.

Return the total priority sum of the optimal subset of tasks.

Constraints

  • 1 <= len(logs) <= 10^5
  • Each log entry is (start_time, end_time, priority) with 0 <= start_time <= end_time <= 10^9 and 1 <= priority <= 10^6.

Examples

Example 1

Input: max_log_utility([[1, 3, 10], [2, 4, 5], [3, 5, 20]])

Output: 30

Example 2

Input: max_log_utility([[1, 2, 5], [2, 3, 7], [3, 4, 4]])

Output: 16

Example 3

Input: max_log_utility([[1, 4, 10], [2, 3, 100], [4, 6, 8], [6, 7, 2]])

Output: 110

Hints

Hint 1

Notice the primary goal is to maximize the number of non-overlapping intervals, and the secondary goal is to maximize the sum of priorities.

Hint 2

Sort the intervals by their end times.

Hint 3

Use Dynamic Programming where each state tracks a tuple (task_count, priority_sum). Use binary search to find the latest compatible previous interval.