Skip to main content

Maximum Total from Optimal Activation Order - Solution & Explanation

MediumArrayTwo PointersGreedySorting6 min readAsked at: Amazon, Google
Practice this problem

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 than limit[i].
  • When you activate the element at index i, it adds value[i] to the total activation value (i.e., the sum of value[i] for all elements that have undergone activation operations).
  • After each activation, if the number of currently active elements becomes x, then all elements j with limit[j] <= x become 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 <= 105
  • 1 <= 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

Try this approach in the editor β†’

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationsO(n!)O(n)Small input sizes or when verifying correctness of other approaches
Greedy with SortingO(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?
The problem is generally classified as Medium. The main difficulty is recognizing that activation order affects the cumulative score and that brute force permutations are impractical. Once the greedy insight is identified, implementing the sorted or heap-based solution is straightforward.
Maximum Total from Optimal Activation Order Python/Java solution
Python solutions usually rely on sorting with the built‑in list sort and the heapq module for priority queues. Java implementations commonly use Arrays.sort and PriorityQueue. Both versions implement the same greedy strategy and achieve O(n log n) runtime.
How to solve Maximum Total from Optimal Activation Order in O(n log n)?
First sort the elements by the metric that determines activation benefit or availability. Iterate through the sorted list while maintaining the best candidates in a max heap if needed. At each step choose the element with the highest contribution and update the running total. Sorting and heap operations together produce an O(n log n) solution.
What is the best approach for Maximum Total from Optimal Activation Order?
The most effective solution uses a greedy strategy combined with sorting or a max heap. Sorting ensures elements with the highest contribution are activated earlier, while a heap handles cases where eligibility changes during iteration. Both implementations typically run in O(n log n) time and avoid the exponential cost of checking all activation orders.
Is Maximum Total from Optimal Activation Order asked at Google/Amazon/Meta?
Greedy scheduling and priority queue optimization problems are commonly asked at companies like Google, Amazon, and Meta. Variants involving optimal ordering, task activation, or maximizing cumulative scores frequently appear in technical interviews because they test greedy reasoning and data structure choice.
What data structure is used in Maximum Total from Optimal Activation Order?
The typical solution relies on arrays for iteration, sorting for ordering decisions, and a priority queue (max heap) to quickly retrieve the element with the largest contribution. Two pointers may also be used to scan the sorted list while dynamically adding eligible elements to the heap.
What is the time complexity of Maximum Total from Optimal Activation Order?
The optimal approach runs in O(n log n) time due to sorting or heap operations. Each element is processed once and inserted or removed from a priority queue at most once. Space complexity is usually O(n) if a heap is used or O(1) auxiliary space if only sorting and iteration are required.

Ready to solve this problem?

Practice Maximum Total from Optimal Activation Order with our built-in code editor and test cases.

Practice on FleetCode