[C++] easy solution - beats 92% runtime, 93% memory
\n\n\n# Code\n\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n = nums.size();\n        int premax[n]; premax[0] = nums[0];
![image.png](https://assets.leetcode.com/users/images/de998dba-ef29-451d-8485-a891c54709bb_1696859269.0507889.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n = nums.size();\n        int premax[n]; premax[0] = nums[0]; //stores prefix max\n        int sufmin[n]; sufmin[n-1] = nums[n-1]; //store suffix min\n        for (int i=1; i<n; i++) premax[i] = max(nums[i], premax[i-1]);\n        for (int i=n-2; i>=0; i--) sufmin[i] = min(nums[i], sufmin[i+1]);\n\n        int ans = 0;\n        for (int i=1; i<=n-2; i++) {\n            if (nums[i] > premax[i-1] && nums[i] < sufmin[i+1]) ans+=2;\n            else if (nums[i] > nums[i-1] && nums[i] < nums[i+1]) ans++;\n        }\n        return ans;\n    }\n};\n```
['Array', 'C++']