3 passes, O(n) space, python
```\nclass Solution:\n    def sumOfBeauties(self, nums: List[int]) -> int:\n        R = nums.copy()\n        L = nums.copy()\n        for i in range(len(nums)-2
```\nclass Solution:\n    def sumOfBeauties(self, nums: List[int]) -> int:\n        R = nums.copy()\n        L = nums.copy()\n        for i in range(len(nums)-2, -1, -1):\n            R[i] = min(R[i], R[i+1])\n        for i in range(1, len(nums)):\n            L[i] = max(L[i], L[i-1])\n        ans = [0]\n        for i in range(1, len(nums)-1):\n            if L[i-1]<nums[i]<R[i+1]:\n                ans.append(2)\n            elif nums[i]>nums[i-1] and nums[i]<nums[i+1]:\n                ans.append(1)\n            else:\n                ans.append(0)\n        return sum(ans)
['Dynamic Programming']