O(N) solution beats 98%
\'\'\'\n\n\n\nclass Solution:\n    def sumOfBeauties(self, nums: List[int]) -> int:\n        n = len(nums)\n        left, right = [False] * n, [False] * n\n    
\'\'\'\n\n\n\nclass Solution:\n    def sumOfBeauties(self, nums: List[int]) -> int:\n        n = len(nums)\n        left, right = [False] * n, [False] * n\n        left_max, right_min = 0, float(\'inf\')\n        \n        \n        for i in range(n):\n            if left_max < nums[i]:\n                left[i] = True\n                left_max = nums[i]\n                \n                \n        for i in range(n - 1, -1, -1):\n            if right_min > nums[i]:\n                right[i] = True\n                right_min = nums[i]\n                \n        ans = 0\n        for i in range(1, n - 1):\n            if left[i] and right[i]:\n                ans += 2\n            elif nums[i] > nums[i - 1] and nums[i] < nums[i + 1]:\n                ans += 1\n        return ans\n\'\'\'
['Python']