leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0665.cpp (600B)
0 class Solution { 1 public: 2 bool checkPossibility(const vector<int> &nums) const { 3 const int n = size(nums); 4 int cnt1 = 0, cnt2 = 0; 5 6 for (int i = 1, prev = nums[0]; i < n && cnt1 <= 1; i++) { 7 if (nums[i] < prev) 8 cnt1++; 9 else 10 prev = nums[i]; 11 } 12 13 if (cnt1 <= 1) return true; 14 15 for (int i = n - 2, prev = nums[n - 1]; i >= 0 && cnt2 <= 1; i--) { 16 if (nums[i] > prev) 17 cnt2++; 18 else 19 prev = nums[i]; 20 } 21 22 return cnt2 <= 1; 23 } 24 };