Leetcode 665. Non-decreasing Array (does not decrement arrays)

Source: Internet
Author: User

Given an array n with integers, your task was to check if it could become non-decreasing by modifying at most 1 ele ment.

We define an array are non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:

41To get a non-decreasing array.

Example 2:

Input: [4,2,1]output:falseexplanation:you can ' t get a non-decreasing array by modify at the most one element.

Note:the n belongs to [1, 10,000].

Title tag: Array

The topic gives us a nums array, allowing us only one chance to change a number so that the array becomes a non-descending array. Return true if it is possible to do so, return false.

The key to this question is, when meeting a nums[i] > nums[i+1], we are to nums[i] to nums[i+1] or nums[i+1] to nums[i].

If feasible, it is of course the choice to prioritize nums[i] to nums[i+1], which reduces the risk of nums[i+1] > nums[i+2].

Take a look at two things:

A. 1 3 5 4 6 7--1 3 4 4 6 7

When it comes to 5 > 4, it is possible to reduce the 5 to 4 because all the numbers before 4:5 are large.

B. 1 4 5 3 6 7--1 4 5 5 6 7

When encountering 5 > 3, here 3:5 is 4 small, so there is no choice, only 3 liters to 5.

  

When a second change is required, you can return false directly, without having to complete the remaining array.

Java Solution:

Runtime beats 89.68%

Completion Date: 10/20/2017

Keywords: Array

Key points: Learn about 2 changes and priorities

1 classSolution2 {3      Public BooleanCheckpossibility (int[] nums)4     {5         BooleanModified =false;6         7          for(inti=0; i<nums.length; i++)8         {9             if(I+1 < nums.length && Nums[i] > nums[i+1])Ten             { One                 if(modified)//if modified a number already A                     return false; -                 Else //if it is first time to modify a number -                 { the                     if(I-1 < 0 | | nums[i+1] >= nums[i-1])//if nums[i+1] is larger or equal all numbers before Nums[i] -Nums[i] = nums[i+1];//Change nums[i] as same as nums[i+1] -                     Else //if nums[i+1] isn't larger than all numbers before Nums[i] -NUMS[I+1] = nums[i];//Change nums[i+1] as same as nums[i] +                      -Modified =true; +                 }  A             } at         } -          -         return true; -          -     } -}

Reference: N/A

Leetcode List of topics-Leetcode Questions list

Leetcode 665. Non-decreasing Array (does not decrement arrays)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.