Skip to main content
Back to problems
#3914
Medium Algorithms

Minimum operations to make array non decreasing

54.8% acceptance
May 13, 2026
48
2
You are given an integer array nums of length n. In one operation, you may choose any subarray nums[l..r] and increase each element in that subarray by x, where x is any positive integer. Return the minimum possible sum of the values of x across all operations required to make the array non-decreasing. An array is non-decreasing if nums[i] <= nums[i + 1] for all 0 <= i < n - 1.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_operations(nums: Vec<i32>) -> i64 {
    let mut total = 0i64;
    for i in 1..nums.len() {
      if nums[i - 1] > nums[i] {
        total += (nums[i - 1] - nums[i]) as i64;
      }
    }
    total
  }
}