Minimum Increment Operations to Make Array Beautiful - Problem
You are given a 0-indexed integer array nums having length n, and an integer k.
You can perform the following increment operation any number of times (including zero):
- Choose an index
iin the range[0, n - 1], and increasenums[i]by1.
An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.
Return an integer denoting the minimum number of increment operations needed to make nums beautiful.
A subarray is a contiguous non-empty sequence of elements within an array.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,3,0,0,2], k = 4
›
Output:
8
💡 Note:
For window [2,3,0], max=3<4, so increment nums[2] to 4 (cost: 4). For window [3,4,0], max=4≥4, no change. For window [4,0,0], max=4≥4, no change. For window [0,0,2], max=2<4, so increment nums[4] to 4 (cost: 2). Total: 4+4=8 operations.
Example 2 — Already Beautiful
$
Input:
nums = [0,0,0,4], k = 4
›
Output:
0
💡 Note:
The only window of size 3+ is [0,0,0,4] which has max=4≥k=4, so no operations needed.
Example 3 — Small Array
$
Input:
nums = [1,2], k = 10
›
Output:
0
💡 Note:
Array has length < 3, so no subarray of size 3+ exists. Already beautiful by definition.
Constraints
- 1 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 109
- 0 ≤ k ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code