Maximal Score After Applying K Operations - Problem
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.
In one operation:
- choose an index
isuch that0 <= i < nums.length, - increase your score by
nums[i], and - replace
nums[i]withceil(nums[i] / 3).
Return the maximum possible score you can attain after applying exactly k operations.
The ceiling function ceil(val) is the least integer greater than or equal to val.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [10,10,10,10,10], k = 5
›
Output:
50
💡 Note:
Pick index 0: score=10, nums=[4,10,10,10,10]. Pick index 1: score=20, nums=[4,4,10,10,10]. Pick index 2: score=30, nums=[4,4,4,10,10]. Pick index 3: score=40, nums=[4,4,4,4,10]. Pick index 4: score=50, nums=[4,4,4,4,4]. Maximum score is 50.
Example 2 — Single Large Element
$
Input:
nums = [1,10,3,3,3], k = 3
›
Output:
17
💡 Note:
Pick 10: score=10, nums=[1,4,3,3,3]. Pick 4: score=14, nums=[1,2,3,3,3]. Pick any 3: score=17, nums=[1,2,1,3,3]. Best strategy is always pick the largest available.
Example 3 — Small Array
$
Input:
nums = [5], k = 2
›
Output:
7
💡 Note:
Pick 5: score=5, nums=[2]. Pick 2: score=7, nums=[1]. Only one element, so must pick from it k times.
Constraints
- 1 ≤ nums.length ≤ 103
- 1 ≤ nums[i] ≤ 106
- 1 ≤ k ≤ 2 × 103
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [10,10,10,10,10] with k=5 operations
2
Process
Each operation: pick max, add to score, reduce by ceil(val/3)
3
Output
Maximum possible score after exactly k operations
Key Takeaway
🎯 Key Insight: Greedy approach works because larger values contribute more to the score, so we should pick them first before they get reduced
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code