bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 665. Non-decreasing Array

665. Non-decreasing Array

Leetcode Solutions

Key approach of the solution: Greedy Algorithm

  1. Initialize a variable modified to false to track if a modification has been made.
  2. Iterate through the array from the second element to the end.
  3. If a violation is found (i.e., nums[i] < nums[i - 1]): a. If modified is already true, return false as we can only modify one element. b. Set modified to true. c. If i == 1 or nums[i - 2] <= nums[i], set nums[i - 1] to nums[i]. d. Otherwise, set nums[i] to nums[i - 1].
  4. If no further violations are found, return true.
UML Thumbnail

Alternative approach of the solution: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...