Easiest c++ solution 0ms beats 100% solve by prefix and suffix
IntuitionApproachComplexity
Time complexity: O(n)

Space complexity: O(n)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

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

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

# Code
```cpp []
class Solution {
public:
    int sumOfBeauties(vector<int>& nums) {
        int ans = 0, mx = nums[0], mn = nums.back();
        vector<int> prefix(nums.size(), 0), suffix(nums.size(), 0);
        for(int i = 1;i < nums.size();i++){
            if(nums[i] > mx){
                prefix[i] = 1;
                mx = nums[i];
            }
        }

        for(int i = nums.size() - 2;i >= 0;i--){
            if(nums[i] < mn){
                suffix[i] = 1;
                mn = nums[i];
            }
        }

        for(int i = 1;i < nums.size() - 1;i++){
            if(prefix[i] + suffix[i] == 2) ans += 2;
            else ans += (nums[i] > nums[i - 1] && nums[i + 1] > nums[i]);
        }

        return ans;

    }
};

// auto init = []()
// { 
//     ios::sync_with_stdio(0);
//     cin.tie(0);
//     cout.tie(0);
//     return 'c';
// }();
```
['Array', 'Suffix Array', 'Prefix Sum', 'C++']