Skip to content

Commit 230f2c9

Browse files
authored
Merge pull request #1612 from kabirdhillon7/665cpp
Create 665-Non-decreasing-Array.cpp
2 parents 2e826d0 + 5677d6d commit 230f2c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

cpp/665-Non-decreasing-Array.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)