Maximize Priority Sum of Non-Overlapping Tasks
Dynamic ProgrammingAuto-graded · 18 test casesLast 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:
- No two chosen tasks overlap in time. Two tasks are allowed to share an endpoint (for example, a task ending at time
3and another starting at time3do not overlap). - The number of selected tasks is maximized.
- If there are multiple valid subsets that achieve the maximum count of tasks, the subset with the highest total
priorityis 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)with0 <= start_time <= end_time <= 10^9and1 <= 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.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be framed as a variation of the Weighted Interval Scheduling problem. However, instead of optimizing just a single scalar objective (like total value or total count), we have a multi-tier objective:
- Maximize the number of selected intervals.
- Maximize the sum of priorities for tie-breaking.
We can model the DP state as a tuple: (task_count, priority_sum). In Python, comparing two tuples (c1, p1) and (c2, p2) natively performs lexicographical comparison—first comparing task_count, and using priority_sum as a tie-breaker.
Algorithm Steps:
- Sort Intervals: Sort all tasks primarily by
end_time. To ensure deterministic ordering, tie-break bystart_timeandpriority. - DP Array: Maintain an array
dpwheredp[i]stores the optimal tuple(count, priority_sum)using a valid subset from the firstisorted tasks. Initializedp[0] = (0, 0). - Binary Search Transition: For each task
iwith(start, end, priority):- Use
bisect_righton the sorted list ofend_timevalues up to indexito find the indexpreviousof the last compatible task whoseend_time <= start. - Option 1 (Include task
i):(dp[previous][0] + 1, dp[previous][1] + priority) - Option 2 (Exclude task
i):dp[i] - Set
dp[i + 1] = max(Option 1, Option 2)using tuple comparison.
- Use
- Result: Return the
priority_sum(the second element) ofdp[n].
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the number of logs. Sorting takes $\mathcal{O}(N \log N)$ time, and for each of the $N$ intervals we perform a binary search in $\mathcal{O}(\log N)$ time.
- Space Complexity: $\mathcal{O}(N)$ to store the sorted intervals, end times, and the
dpstate table.
Python Reference Solution
from bisect import bisect_right
from typing import List, Tuple
def max_log_utility(logs: List[Tuple[int, int, int]]) -> int:
intervals = sorted(logs, key=lambda item: (item[1], item[0], item[2]))
n = len(intervals)
ends = [end for _, end, _ in intervals]
dp = [(0, 0)] # (count, priority_sum)
for i, (start, end, priority) in enumerate(intervals):
previous = bisect_right(ends, start, 0, i)
included = (dp[previous][0] + 1, dp[previous][1] + priority)
excluded = dp[i]
dp.append(included if included > excluded else excluded)
return dp[n][1]