https://leetcode.com/problems/non-decreasing-array/description/
bool checkPossibility(vector<int>& nums) {
nums.insert(nums.begin(), INT_MIN);
nums.push_back(INT_MAX);
int dec = 0, idx = -1;
for(int i=1; i<nums.size(); i++) {
if(nums[i-1]>nums[i]) {
dec++;
if(dec == 1) idx = i;
}
if(dec>1) return false;
}
if(dec == 1)
return (idx>1 && nums[idx]>=nums[idx-2])
|| (idx+1<nums.size() && nums[idx+1]>=nums[idx-1]);
return true;
}
No comments:
Post a Comment