Leetcode 665 Solution
This article provides solution to leetcode question 665 (non-decreasing-array)
Access this page by simply typing in "lcs 665" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/non-decreasing-array
Solution
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int state = 0;
int last_val = -100000;
for (int i = 0; i < nums.size(); i++)
{
int val = nums[i];
if (val < last_val)
{
if (state != 0)
return false;
if (i == 1 || val >= nums[i - 2])
last_val = val;
state = 1;
}
else
last_val = val;
}
return true;
}
};