C++, Simple O(n), Easy to understand
class Solution {\npublic:\n    int sumOfBeauties(vector& ar) {\n        \n        int n =ar.size(), ans = 0, l[n], r[n];\n        \n        l[0]=ar[0];\n       
class Solution {\npublic:\n    int sumOfBeauties(vector<int>& ar) {\n        \n        int n =ar.size(), ans = 0, l[n], r[n];\n        \n        l[0]=ar[0];\n        r[n-1]=ar[n-1];\n        \n        for(int i=1;i<n;i++){\n            l[i]=max(l[i-1],ar[i]);\n            r[n-1-i]=min(r[n-i],ar[n-1-i]);\n        }\n        \n        for(int i=1;i<=n-2;i++)\n            if(l[i-1]<ar[i] && ar[i]<r[i+1])\n                ans+=2;\n            else if(ar[i-1]<ar[i] && ar[i]<ar[i+1])\n                ans+=1;\n        \n        return ans;\n    }\n};
[]