↓Skip to main content
  1. LeetCode/

LeetCode 3914: Minimum Operations to Make Array Non Decreasing

·2 mins· ·
LeetCode Medium Greedy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-26

  • Submissions: Accepted (first try)

Approach
#

Look at adjacent differences d[i] = nums[i] - nums[i - 1]; the goal is every d[i] >= 0. Adding x to [l, r]:

  • raises d[l] by x (if l > 0),
  • lowers d[r + 1] by x (if r + 1 < n),
  • leaves the differences in between unchanged.

So one operation raises at most one difference by x. Each drop nums[i - 1] - nums[i] > 0 needs at least that much, and their sum is a lower bound. Adding to the suffix [i, n - 1] fixes exactly d[i] without breaking anything, reaching the bound. This is Greedy Template 5.

My Solution
#

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        return sum(max(0, nums[i - 1] - nums[i]) for i in range(1, len(nums)))

I originally accumulated the drops right to left in a while loop, which is the same thing.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • No need to simulate the additions; adjacent differences give the answer directly.

Interview Explanation
#

Adding x to a range only raises the difference at its left edge by x, so each drop costs at least its size. Adding to a suffix fixes exactly one drop, so the answer is the sum of all drops. Time O(n).

Related

LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 3810: Minimum Operations to Reach Target Array
·2 mins
LeetCode Medium Greedy Hash-Table Array
LeetCode 3810 note: one operation fixes every position holding a value, so the answer is the number of distinct values at mismatched positions
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially