Advertisement

Non-decreasing Array - LeetCode 665 Solution

Non-decreasing Array - Complete Solution Guide

Non-decreasing Array is LeetCode problem 665, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element . We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i ( 0-based ) such that ( 0 <= i <= n - 2 ). Example 1: Input: nums = [4,2,3] Output: true Explanation: You could modify the first 4 to 1 to get a non-decreasing array. Example 2: Input: nums = [4,2,1] Output: false Explanation: You cannot get a non-decreasing array by modifying at most one e

Detailed Explanation

The problem asks us to determine if an array of integers can be made non-decreasing by modifying at most one element. A non-decreasing array is one where each element is less than or equal to the element that follows it (nums[i] <= nums[i+1] for all i). The input is an array of integers `nums`, and the output is a boolean value: `true` if the array can be made non-decreasing with at most one modification, and `false` otherwise. The constraints are that the array length is between 1 and 10,000, and the integer values are between -100,000 and 100,000.

Solution Approach

The provided solution iterates through the array, checking for instances where `nums[i] > nums[i+1]`. If such an instance is found, it increments the `modifications` counter. If the counter exceeds 1, it immediately returns `false`. The crucial part is deciding how to modify the array. The code checks if `i > 0` and `nums[i-1] > nums[i+1]`. If this condition is true, it means modifying `nums[i]` is not a good option as it will not solve the non-decreasing property with respect to the previous number `nums[i-1]`. Instead, we modify `nums[i+1]` to be equal to `nums[i]`. Otherwise, it means modifying `nums[i]` is the best choice, and sets it equal to `nums[i+1]`. Finally, if it iterates through the whole array without exceeding the one modification limit, it returns `true`.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable `modifications` to 0. This variable will track the number of modifications made to the array.
  2. Step 2: Iterate through the array from index 0 to `len(nums) - 2`. In each iteration, compare `nums[i]` with `nums[i + 1]`.
  3. Step 3: If `nums[i] > nums[i + 1]`, a violation of the non-decreasing order is found.
  4. Step 4: Check if `modifications > 0`. If it is, it means we have already made one modification, and making another would exceed the limit, so return `false`.
  5. Step 5: Increment `modifications` by 1.
  6. Step 6: Determine which element to modify: `nums[i]` or `nums[i + 1]`. If `i > 0` and `nums[i - 1] > nums[i + 1]`, then modifying `nums[i]` to `nums[i+1]` is not feasible because it violates non-decreasing order between `nums[i-1]` and `nums[i]`. So set `nums[i + 1] = nums[i]`. Otherwise, set `nums[i] = nums[i + 1]`.
  7. Step 7: After iterating through the entire array, if the number of `modifications` is at most 1, return `true`.

Key Insights

  • Insight 1: The core idea is to iterate through the array and check for violations of the non-decreasing order (nums[i] > nums[i+1]).
  • Insight 2: When a violation is found, we have two options: either modify nums[i] or nums[i+1] to resolve the violation. Choosing the correct one is crucial.
  • Insight 3: We need to keep track of the number of modifications made. If we encounter more than one violation, the array cannot be made non-decreasing with a single modification.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array.

Companies

Asked at: Cashfree.