JAVA || PREFIX & SUFFIX ARRAY || TIME : O(N) || SPACE : O(2N)
\nclass Solution {\n    public int sumOfBeauties(int[] nums) \n    {\n        int n=nums.length;\n        int max_left[]=new int[n];\n        int min_right[]=ne
```\nclass Solution {\n    public int sumOfBeauties(int[] nums) \n    {\n        int n=nums.length;\n        int max_left[]=new int[n];\n        int min_right[]=new int[n];\n        int ans=0;\n        \n        for(int i=1;i<n;i++)\n        {\n            max_left[i]=Math.max(max_left[i-1],nums[i-1]);\n        }\n        \n        min_right[n-1]=nums[n-1];\n        for(int i=n-2;i>=0;i--)\n        {\n            min_right[i]=Math.min(min_right[i+1],nums[i+1]);\n        }\n        \n        for(int i=1;i<n-1;i++)\n        {\n            if(nums[i]>max_left[i] && nums[i]<min_right[i])\n            ans+=2;\n            \n            else if(nums[i]>nums[i-1] && nums[i]<nums[i+1])\n            ans++;\n        }\n        \n        return ans;\n    }\n}\n```
['Array', 'Suffix Array', 'Java']