<p>Given an array <code>nums</code> with <code>n</code> integers, your task is to check if it could become non-decreasing by modifying <strong>at most one element</strong>.</p>

<p>We define an array is non-decreasing if <code>nums[i] &lt;= nums[i + 1]</code> holds for every <code>i</code> (<strong>0-based</strong>) such that (<code>0 &lt;= i &lt;= n - 2</code>).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,2,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> You could modify the first 4 to 1 to get a non-decreasing array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,2,1]
<strong>Output:</strong> false
<strong>Explanation:</strong> You cannot get a non-decreasing array by modifying at most one element.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        vector<int> v;
        int f = 0;
        v.push_back(nums[0]);

        for(int i = 1; i < nums.size(); i++) {
            std::vector<int>::iterator itr = upper_bound(v.begin(), v.end(), nums[i]);
            // if (itr == v.end()) {
            //     if (v[v.size() - 1] == nums[i]) continue;
            //     v.push_back(nums[i]);
            // } else {
            //     cout<<i<<" ";
            //     if (f) return false;
            //     f = 1;
            //     if (v.size() == (itr - v.begin() + 1)) {
            //         if (i == 1) {
            //             v[0] = min(v[0], nums[i]);
            //         } else {
            //             v.push_back(v[v.size() - 1] + 1);
            //         }
            //     } else {
            //         if (itr == v.begin()) {
            //             continue;
            //         }
            //         return false;
            //     }
                
            // }
            if (itr == v.end()) {
                v.push_back(nums[i]);
            } else {
                if (f) return false;
                f = 1;
                if (v.size() == (itr - v.begin() + 1)) {
                    v[v.size() - 1] = nums[i];
                } else {
                    v.push_back(v[v.size() - 1]);
                }
            }
        }
        // cout<<"\n";

        return true;
    }
};
30400