Simple O(n) C++ Solution and No Modifying


  • 0
    class Solution {
    public:
        bool checkPossibility(vector<int>& nums) {
            // everytime discard the broken point
            int count = 0;
            int first = INT_MIN, second = INT_MIN;
            for (int i = 0; i < nums.size(); ++i) {
                if (nums[i] >= first) {
                    second = first;
                    first = nums[i];
                    continue;
                } else if (nums[i] >= second) {
                    first = nums[i];
                }
                if (++count > 1) return false; 
            }
            return true;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.