<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. For each index <code>i</code> (<code>1 &lt;= i &lt;= nums.length - 2</code>) the <strong>beauty</strong> of <code>nums[i]</code> equals:</p>

<ul>
	<li><code>2</code>, if <code>nums[j] &lt; nums[i] &lt; nums[k]</code>, for <strong>all</strong> <code>0 &lt;= j &lt; i</code> and for <strong>all</strong> <code>i &lt; k &lt;= nums.length - 1</code>.</li>
	<li><code>1</code>, if <code>nums[i - 1] &lt; nums[i] &lt; nums[i + 1]</code>, and the previous condition is not satisfied.</li>
	<li><code>0</code>, if none of the previous conditions holds.</li>
</ul>

<p>Return<em> the <strong>sum of beauty</strong> of all </em><code>nums[i]</code><em> where </em><code>1 &lt;= i &lt;= nums.length - 2</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> For each index i in the range 1 &lt;= i &lt;= 1:
- The beauty of nums[1] equals 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,4,6,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> For each index i in the range 1 &lt;= i &lt;= 2:
- The beauty of nums[1] equals 1.
- The beauty of nums[2] equals 0.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> For each index i in the range 1 &lt;= i &lt;= 1:
- The beauty of nums[1] equals 0.
</pre>

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

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

class Solution:
    def sumOfBeauties(self, nums: List[int]) -> int:
        #Approach-1 O(N^2) Time Approach
        '''n=len(nums)
        cnt=0
        for i in range(1,n-1):
            l=i-1
            r=i+1
            lmin=True
            rmax=True
            while l>=0:
                if nums[i]<=nums[l]:
                    lmin=False
                    break
                l-=1
            
            while r<n:
                if nums[i]>=nums[r]:
                    rmax=False
                    break
                r+=1
            
            if lmin and rmax: cnt+=2
            elif nums[i-1]<nums[i]<nums[i+1]: cnt+=1
        return cnt'''


        #Approach-2 O(N) time and O(N) space

        #find prefix max and suffix min at index i

        n=len(nums)
        pm=[float("-inf") for _ in range(n)]
        sm=[float("inf") for _ in range(n)]
        pm[0]=nums[0]
        sm[n-1]=nums[n-1]
        for i in range(1,n):
            pm[i]=max(nums[i],pm[i-1])
            j=n-1-i
            sm[j]=min(nums[j],sm[j+1])
        
        cnt=0
        for i in range(1,n-1):
            if pm[i-1]<nums[i]<sm[i+1]:
                cnt+=2
            elif nums[i-1]<nums[i]<nums[i+1]:
                cnt+=1
        return cnt
        

858