Simple Java Solution beats 98% | O(n) Time and Space
Intuition\nMaintain 2 arrays, first to store if an element is greater than all elements to the left of it, and other to store if an element is smaller than all 
# Intuition\nMaintain 2 arrays, first to store if an element is greater than all elements to the left of it, and other to store if an element is smaller than all elements to the right of it.\n\n# Approach\n1. Declare two boolean arrays of size=nums.length.\n2. Iterate through array from the left to the right side and check if an element is an element is greater than the maximum element on its left side. If so, set the value of that index in the first declared array to true.\n3. Iterate through array from the right to the left side and check if an element is an element is lesser than the minimum element on its right side. If so, set the value of that index in the second declared array to true.\n4. Iterate through nums, if for an index the corresponding index in both the declared arrays is set to true, add 2 to some. Otherwise if the element is greater than its neighbours, add 1 to the sum.\nReturn the sum.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    public int sumOfBeauties(int[] nums) {\n\n        boolean[] prefixMaxLeft = new boolean[nums.length], prefixMinRight = new boolean[nums.length];\n        int minFromRight = nums[nums.length - 1], maxFromLeft = nums[0];\n        int sum = 0;\n\n        for (int i = 1; i < nums.length - 1; i++) {\n\n            if (maxFromLeft < nums[i]) {\n                prefixMaxLeft[i] = true;\n                maxFromLeft = nums[i];\n            }\n\n            if (nums[nums.length - 1 - i] < minFromRight) {\n                prefixMinRight[nums.length - 1 - i] = true;\n                minFromRight = nums[nums.length - 1 - i];\n            }\n        }\n\n        for (int i = 1; i < nums.length - 1; i++) {\n            if (prefixMaxLeft[i] && prefixMinRight[i]) {\n                sum = sum + 2;\n            } else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {\n                sum = sum + 1;\n            }\n        }\n\n        return sum;\n    }\n}\n```\n\nFeel free to comment any optimizations.\nPlase upvote if found helpful!
['Array', 'Java']