665. Non-decreasing array allows only one-digit non-descending array to be modified

Source: Internet
Author: User

[Copy question]:

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.

[Brute force solution]:

Time Analysis:

Spatial Analysis:

[After optimization]:

Time Analysis:

Spatial Analysis:

[Wonderful output CONDITIONS]:

[Wonderful corner case]:

[Thinking questions]:

I don't know how to change it.

[a sentence of thought]:

Since only allowed to modify a bit, "the day before yesterday " is unusual, decided that should be modified "Yesterday" or "Today"

[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):

[Drawing]:

[One brush]:

    1. There must be a precondition for "Yesterday" exception to follow-up operations. So we're going to wrap everything after "Yesterday".

[Two brushes]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

[Results of five-minute visual debug]:

[Summary]:

First see: Since only allowed to modify a bit, "the day before yesterday " is unusual, decided that should be modified "Yesterday" or "Today"

[Complexity]:time Complexity:o (n) Space complexity:o (1)

[English data structure or algorithm, why not other data structures or algorithms]:

[Key templating code]:

[Other solutions]:

[Follow up]:

[The problem given by the LC becomes variable]:

[Code Style]:

classSolution { Public BooleanCheckpossibility (int[] nums) {        //cc        if(Nums = =NULL|| Nums.length = = 0) {            return false; }                //INI        intCount = 0; //For Loop         for(inti = 1; I < Nums.length && count <= 1; i++) {            if(Nums[i-1] > Nums[i]) {count++; if(I-2 < 0 | | nums[i-2] <Nums[i]) {Nums[i-1] =Nums[i]; }Else{Nums[i]= Nums[i-1]; }}        }                //return count <= 1        returnCount <= 1; }}
View Code

665. Non-decreasing array allows only one-digit non-descending array to be modified

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.