Maximum Total from Optimal Activation Order - Solution & Explanation
Problem Statement
You are given two integer arrays value and limit, both of length n.
Initially, all elements are inactive. You may activate them in any order.
- To activate an inactive element at index
i, the number of currently active elements must be strictly less thanlimit[i]. - When you activate the element at index
i, it addsvalue[i]to the total activation value (i.e., the sum ofvalue[i]for all elements that have undergone activation operations). - After each activation, if the number of currently active elements becomes
x, then all elementsjwithlimit[j] <= xbecome permanently inactive, even if they are already active.
Return the maximum total you can obtain by choosing the activation order optimally.
Example 1:
Input: value = [3,5,8], limit = [2,1,3]
Output: 16
Explanation:
One optimal activation order is:
| Step | Activated i |
value[i] |
Active Before i |
Active After i |
Becomes Inactive j |
Inactive Elements | Total |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 5 | 0 | 1 | j = 1 as limit[1] = 1 |
[1] | 5 |
| 2 | 0 | 3 | 0 | 1 | - | [1] | 8 |
| 3 | 2 | 8 | 1 | 2 | j = 0 as limit[0] = 2 |
[0, 1] | 16 |
Thus, the maximum possible total is 16.
Example 2:
Input: value = [4,2,6], limit = [1,1,1]
Output: 6
Explanation:
One optimal activation order is:
| Step | Activated i |
value[i] |
Active Before i |
Active After i |
Becomes Inactive j |
Inactive Elements | Total |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 6 | 0 | 1 | j = 0, 1, 2 as limit[j] = 1 |
[0, 1, 2] | 6 |
Thus, the maximum possible total is 6.
Example 3:
Input: value = [4,1,5,2], limit = [3,3,2,3]
Output: 12
Explanation:
One optimal activation order is:ββββββββββββββ
| Step | Activated i |
value[i] |
Active Before i |
Active After i |
Becomes Inactive j |
Inactive Elements | Total |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 5 | 0 | 1 | - | [ ] | 5 |
| 2 | 0 | 4 | 1 | 2 | j = 2 as limit[2] = 2 |
[2] | 9 |
| 3 | 1 | 1 | 1 | 2 | - | [2] | 10 |
| 4 | 3 | 2 | 2 | 3 | j = 0, 1, 3 as limit[j] = 3 |
[0, 1, 2, 3] | 12 |
Thus, the maximum possible total is 12.
Constraints:
1 <= n == value.length == limit.length <= 1051 <= value[i] <= 105βββββββ1 <= limit[i] <= n
Approach Overview
Problem Overview: You are given an array where each element contributes to the total when it becomes activated. The order of activation changes the final score, so the goal is to choose an activation sequence that maximizes the total value. The challenge is identifying which elements should be activated earlier versus later to maximize cumulative gain.
Approach 1: Brute Force Permutation (O(n!))
The most direct method is to generate every possible activation order and compute the resulting total for each permutation. For every order, iterate through the array and simulate the activation step by step while accumulating the contribution. The maximum across all permutations becomes the answer. This approach clearly demonstrates how activation order affects the result but becomes infeasible very quickly because permutations grow factorially. Time complexity is O(n!) and space complexity is O(n) for recursion or permutation storage.
Approach 2: Greedy with Sorting (O(n log n))
The key insight is that elements that produce larger contributions should generally be activated earlier. By sorting the array based on the metric that determines contribution (often value or gain potential), you ensure high-impact activations happen first. After sorting, iterate through the array and accumulate the total while maintaining any running contribution needed by the scoring rule. Sorting dominates the runtime with O(n log n) time and O(1) or O(n) auxiliary space depending on the language implementation. This greedy ordering works because each step locally maximizes the gain without harming future choices.
Approach 3: Greedy with Heap / Priority Queue (O(n log n))
When the activation decision depends on dynamic conditions (for example, only some elements are eligible at each step), a heap (priority queue) becomes useful. First sort elements by the constraint that determines when they become available. Use two pointers to scan the sorted list while pushing eligible candidates into a max-heap. At each step, extract the element with the highest contribution and activate it next. The heap ensures the best candidate is always chosen among currently valid options. Each push and pop costs O(log n), giving an overall time complexity of O(n log n) and space complexity O(n). This pattern often appears in scheduling and resource allocation problems.
Recommended for interviews: Interviewers expect the greedy reasoning that leads to sorting or a priority queue. Starting with brute force shows you understand that order matters, but the real signal of problem-solving ability is recognizing that a greedy strategy with sorting or a greedy heap selection reduces the search space to O(n log n). That optimization is the typical production-ready solution.
Solution
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutations | O(n!) | O(n) | Small input sizes or when verifying correctness of other approaches |
| Greedy with Sorting | O(n log n) | O(1)βO(n) | When activation benefit depends primarily on element value or contribution ranking |
| Greedy with Heap (Priority Queue) | O(n log n) | O(n) | When elements become eligible dynamically and the best candidate must be chosen each step |
Video Solution
Leetcode 3645 | Maximum Total from Optimal Activation Order Detailed Explanation | Contest 462 Q3 β’ Samrat Bhardwaj β’ 628 views views
Watch 6 more video solutions βFrequently Asked Questions
Is Maximum Total from Optimal Activation Order easy or hard?
Maximum Total from Optimal Activation Order Python/Java solution
How to solve Maximum Total from Optimal Activation Order in O(n log n)?
What is the best approach for Maximum Total from Optimal Activation Order?
Is Maximum Total from Optimal Activation Order asked at Google/Amazon/Meta?
What data structure is used in Maximum Total from Optimal Activation Order?
What is the time complexity of Maximum Total from Optimal Activation Order?
Ready to solve this problem?
Practice Maximum Total from Optimal Activation Order with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor