3 ms | Beats 76.33% | maxprefix | minsuffix 
🔹 IntuitionWe need to calculate the beauty of each element nums[i] in an array based on the following rules:

If nums[i] is strictly greater than all elements b
# 🔹 Intuition
We need to calculate the beauty of each element nums[i] in an array based on the following rules:

- If `nums[i] is strictly greater` than all elements before it and strictly smaller than all elements after it, `add 2 to the sum`.

- If `nums[i] is greater than nums[i−1] and smaller than nums[i+1]`, `add 1 to the sum`.

- Otherwise, add `0`.

To efficiently find elements satisfying the first condition, we use:

- Prefix Maximum Array (pre[i]) → Stores the `maximum value` from `nums[0] to nums[i]`

- Suffix Minimum Array (suff[i]) → Stores the `minimum value` from `nums[i] to nums[n-1]`

This allows us to check conditions in` O(1)` time for each nums[i].
<!-- Describe your first thoughts on how to solve this problem. -->

# 🔹 Approach
1. Compute pre[i] (Prefix Maximum Array)

    - `pre[i] = max(pre[i-1], nums[i])`

2. Compute suff[i] (Suffix Minimum Array)

    - `suff[i] = min(suff[i+1], nums[i])`

3. Iterate through nums[i] (from 1 to n-2)

    - If `pre[i-1] < nums[i] < suff[i+1],` add `2`.
    - Else, if `nums[i-1] < nums[i] < nums[i+1]`, add `1`.


<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: $$O(n)$$ 
<!-- Add your time complexity here, e.g.-->

- Space complexity:$$O(n)$$
<!-- Add your space complexity here, e.g.  -->

# Code
```cpp []
class Solution {
public:
    int sumOfBeauties(vector<int>& nums) {
        int maxsum=0;
        int n=nums.size();
        vector<int> pre(n,0);
        vector<int> suff(n,0);
    int a=nums[0];
        for(int i=0;i<n;i++){
            if(nums[i]>a){
                a=nums[i];
            }
            pre[i]=a;
        }
        a=nums[n-1];
        for(int i=n-1;i>=0;i--){
            if(nums[i]<a){
                a=nums[i];
            }
            suff[i]=a;
        }
        for(int i=1;i<n-1;i++){
            if(pre[i-1]<nums[i]&& nums[i]<suff[i+1])maxsum+=2;
            else if(nums[i-1]<nums[i]&&  nums[i]<nums[i+1])maxsum+=1;
        }
        return maxsum;
    }
};
```
```java []
class Solution {
    public int sumOfBeauties(int[] nums) {
        int n = nums.length, maxsum = 0;
        int[] pre = new int[n], suff = new int[n];

        // Compute prefix max array
        pre[0] = nums[0];
        for (int i = 1; i < n; i++)
            pre[i] = Math.max(pre[i - 1], nums[i]);

        // Compute suffix min array
        suff[n - 1] = nums[n - 1];
        for (int i = n - 2; i >= 0; i--)
            suff[i] = Math.min(suff[i + 1], nums[i]);

        // Compute beauty sum
        for (int i = 1; i < n - 1; i++) {
            if (pre[i - 1] < nums[i] && nums[i] < suff[i + 1])
                maxsum += 2;
            else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1])
                maxsum += 1;
        }

        return maxsum;
    }
}

```
```py []
class Solution:
    def sumOfBeauties(self, nums: List[int]) -> int:
        n = len(nums)
        pre = [0] * n
        suff = [0] * n

        # Compute prefix max array
        pre[0] = nums[0]
        for i in range(1, n):
            pre[i] = max(pre[i - 1], nums[i])

        # Compute suffix min array
        suff[n - 1] = nums[n - 1]
        for i in range(n - 2, -1, -1):
            suff[i] = min(suff[i + 1], nums[i])

        # Compute beauty sum
        maxsum = 0
        for i in range(1, n - 1):
            if pre[i - 1] < nums[i] < suff[i + 1]:
                maxsum += 2
            elif nums[i - 1] < nums[i] < nums[i + 1]:
                maxsum += 1

        return maxsum

```
['Array', 'C++', 'Java', 'Python3']