#3767
Maximize Points After Choosing K Tasks
MediumArrayGreedySortingHeap (Priority Queue)GreedySortingHeap
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n log n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n log n)Space O(n)
Start by completing all tasks with technique1, then selectively switch to technique2 for the tasks that yield the highest delta in points.
⚙️
Algorithm
3 steps- 1Step 1: Calculate the initial points using all tasks from technique1.
- 2Step 2: Create a list of deltas (points gained by switching to technique2) and sort it in descending order.
- 3Step 3: Add the top (n-k) deltas to the initial points to maximize the total.
solution.py7 lines
1def maxPoints(technique1, technique2, k):
2 n = len(technique1)
3 total_points = sum(technique1)
4 deltas = [technique2[i] - technique1[i] for i in range(n)]
5 deltas.sort(reverse=True)
6 total_points += sum(deltas[i] for i in range(n - k))
7 return total_pointsℹ
Complexity note: Sorting the deltas takes O(n log n), and the space is used for the deltas array.
- 1Maximize points by strategically choosing tasks.
- 2Sorting helps identify the best tasks to switch techniques.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.