[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:
4
1
To 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]:
- 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