---
# System prepended metadata

title: 2012. Sum of Beauty in the Array
tags: [Leetcode, Medium]

---

# 2012. Sum of Beauty in the Array
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/sum-of-beauty-in-the-array/description/
## 思路
precomputation算出每个位置左边的最大值和右边的最小值 即可判断出哪个位置的point要加2
## Code
```python=
class Solution:
    def sumOfBeauties(self, nums: List[int]) -> int:
        n = len(nums)
        maxVal = [nums[0]]*n
        minVal = [nums[-1]]*n
        beauty = 0
        for i in range(1, n-1):
            maxVal[i] = (max(maxVal[i-1], nums[i]))
        for i in range(n-2, 0, -1):
            minVal[i] = (min(minVal[i+1], nums[i]))
        for i in range(1, n-1):
            if maxVal[i-1]<nums[i] and minVal[i+1]>nums[i]: beauty += 2
            elif nums[i-1]<nums[i]<nums[i+1]: beauty += 1
        return beauty    
        
```