You are given two integer arrays, technique1 and technique2, each of length n, where n represents the number of tasks to complete.
ith task is completed using technique 1, you earn technique1[i] points.technique2[i] points.You are also given an integer k, representing the minimum number of tasks that must be completed using technique 1.
You must complete at least k tasks using technique 1 (they do not need to be the first k tasks).
The remaining tasks may be completed using either technique.
Return an integer denoting the maximum total points you can earn.
Example 1:
Input: technique1 = [5,2,10], technique2 = [10,3,8], k = 2
Output: 22
Explanation:
We must complete at least k = 2 tasks using technique1.
Choosing technique1[1] and technique1[2] (completed using technique 1), and technique2[0] (completed using technique 2), yields the maximum points: 2 + 10 + 10 = 22.
Example 2:
Input: technique1 = [10,20,30], technique2 = [5,15,25], k = 2
Output: 60
Explanation:
We must complete at least k = 2 tasks using technique1.
Choosing all tasks using technique 1 yields the maximum points: 10 + 20 + 30 = 60.
Example 3:
Input: technique1 = [1,2,3], technique2 = [4,5,6], k = 0
Output: 15
Explanation:
Since k = 0, we are not required to choose any task using technique1.
Choosing all tasks using technique 2 yields the maximum points: 4 + 5 + 6 = 15.
Constraints:
1 <= n == technique1.length == technique2.length <= 1051 <= technique1[i], technique2[i] <= 1050 <= k <= nProblem Overview: You are given a list of tasks where each task has a requirement and a reward (points). You can complete at most k tasks, and a task becomes available only when your current points meet its requirement. The goal is to pick tasks in an order that maximizes the total points after completing up to k tasks.
Approach 1: Brute Force / Backtracking (Exponential Time)
Try every possible sequence of up to k tasks. At each step, iterate through all remaining tasks and pick one whose requirement is less than or equal to the current points. Recursively continue while tracking the best score. This approach quickly becomes infeasible because the number of permutations grows factorially. Time complexity is O(n!) in the worst case with O(n) recursion space.
Approach 2: Greedy + Sorting + Max Heap (Optimal) (O(n log n))
Sort tasks by their requirement so you can efficiently identify which tasks become available as your points increase. Iterate up to k times and push the reward of every task whose requirement is <= current points into a max heap. The heap always keeps the highest reward available at the top. Each round, pop the maximum reward task and add its points to your total. This greedy decision works because picking the largest reward among currently feasible tasks maximizes future opportunities.
The algorithm processes tasks in requirement order while the heap handles reward prioritization. Each task is inserted and removed from the heap at most once. That results in O(n log n) time due to sorting and heap operations, and O(n) space for the heap.
Recommended for interviews: The greedy strategy with sorting and a max heap is what interviewers expect. It shows you can combine greedy algorithms with sorting and a priority queue to manage dynamically available choices. Mentioning the brute force idea first demonstrates understanding of the search space, while the heap optimization proves you can reduce it to O(n log n).
We can first assign all tasks to technique 2, so the initial total score is sum_{i=0}^{n-1} technique2[i].
Then, we calculate the score increase for each task if it were completed using technique 1 instead, denoted as diff[i] = technique1[i] - technique2[i]. We sort this in descending order to obtain a sorted array of task indices idx.
Next, we select the first k tasks to be completed using technique 1 and add their score differences to the total score. For the remaining tasks, if a task can increase the score by using technique 1 (i.e., diff[i] geq 0), we also choose to complete it using technique 1.
The time complexity is O(n times log n), and the space complexity is O(n). Where n is the number of tasks.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force / Backtracking | O(n!) | O(n) | Conceptual baseline to understand all possible task orders |
| Greedy + Sorting + Max Heap | O(n log n) | O(n) | General optimal solution when tasks unlock based on current points |
Maximize Points After Choosing K Tasks | LeetCode 3767 | Biweekly Contest 171 • Sanyam IIT Guwahati • 458 views views
Watch 6 more video solutions →Practice Maximize Points After Choosing K Tasks with our built-in code editor and test cases.
Practice on FleetCode