We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 2e826d0 + 5677d6d commit 230f2c9Copy full SHA for 230f2c9
cpp/665-Non-decreasing-Array.cpp
@@ -0,0 +1,25 @@
1
+class Solution {
2
+public:
3
+ bool checkPossibility(vector<int>& nums) {
4
+ bool changed = false;
5
+
6
+ for(int i = 0; i < nums.size() - 1; i++){
7
+ if(nums[i] <= nums[i+1]){
8
+ continue;
9
+ }
10
+ if(changed){
11
+ return false;
12
13
14
+ if(i == 0 || nums[i+1] >= nums[i-1]){
15
+ nums[i] = nums[i+1];
16
17
+ else{
18
+ nums[i+1] = nums[i];
19
20
+ changed = true;
21
22
23
+ return true;
24
25
+};
0 commit comments