<p>You are given an integer array <code>arr</code>.</p>

<p>We split <code>arr</code> into some number of <strong>chunks</strong> (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.</p>

<p>Return <em>the largest number of chunks we can make to sort the array</em>.</p>

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

<pre>
<strong>Input:</strong> arr = [5,4,3,2,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn&#39;t sorted.
</pre>

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

<pre>
<strong>Input:</strong> arr = [2,1,3,4,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 2000</code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        cin.tie(NULL);
        cout.tie(NULL);
        // int ans =0;
        // for(int i =0;i<arr.size()-1;i++){
        //     vector<int>left(arr.begin(),arr.begin()+i+1);
        //     vector<int>right(arr.begin()+i+1,arr.end());
        //     sort(left.begin(),left.end());
        //     sort(right.begin(),right.end());
        //     if(left.back() <=right[0])ans++;
        // }
        // return ans+1;
        vector<int>mini;
        vector<int>maxi;
        maxi.push_back(arr[0]);
        mini.push_back(arr.back());
        for(int i =1;i<arr.size();i++){
            maxi.push_back(max(maxi.back(),arr[i]));
        }
        for(int i =arr.size()-2;i>=0;i--){
            mini.push_back(min(mini.back(),arr[i]));
        }
        reverse(mini.begin(),mini.end());
        int ans=0;
        for(int i =0;i<arr.size()-1;i++){
            if(maxi[i]<=mini[i+1])ans++;
        }
        return ans+1;
        
    }
};
15700