2530. Maximal Score After Applying K Operations
MediumView on LeetCode
Time: O(n + k log n)
Space: O(n)
Problem Overview
Maximal Score After Applying K Operations (Medium) asks you to solve a structured algorithmic task. This is a common Array / Greedy pattern in coding interviews. Max-heap; k times take max element, add to score, push ceil(max/3) back.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Max-heap; k times take max element, add to score, push ceil(max/3) back.
Related patterns: Array, Greedy, Heap (Priority Queue)
2530.cs
C#
// Approach: Max-heap; k times take max element, add to score, push ceil(max/3) back.
// Time: O(n + k log n) Space: O(n)
public class Solution
{
public long MaxKelements(int[] nums, int k)
{
long ans = 0;
PriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>();
foreach (var num in nums)
maxHeap.Enqueue(num, -num);
for (int i = 0; i < k; ++i)
{
int num = maxHeap.Dequeue();
ans += num;
maxHeap.Enqueue((num + 2) / 3, -(num + 2) / 3);
}
return ans;
}
}Was this solution helpful?