O(N) time O(N) space
```class Solution {\npublic:\n    int sumOfBeauties(vector& nums) {\n        int n = nums.size();\n        vectorsmin(n,nums[n-1]);\n        for(int i=n-2;i>=0;
```class Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n = nums.size();\n        vector<int>smin(n,nums[n-1]);\n        for(int i=n-2;i>=0;i--)\n            smin[i] = min(smin[i+1],nums[i+1]);\n        int pmax = nums[0];\n        int ans = 0;\n        for(int i=1;i<n-1;i++){\n            if(nums[i]>pmax && nums[i] < smin[i])\n                ans += 2;\n            else if(nums[i]> nums[i-1] && nums[i] < nums[i+1])\n                ans += 1;\n            \n            pmax = max(pmax,nums[i]);\n        }\n        return ans;\n    }\n};
[]