#2012
Sum of Beauty in the Array
specialist · 860 · lc medium +31 · verified · 51.1% accepted · 683 likes · top 40%
Description
For each interior index i (1 <= i <= nums.length - 2) of the 0-indexed array nums, its beauty score is:
- 2 if nums[i] is strictly greater than every element to its left and strictly less than every element to its right;
- 1 if only the immediate neighbors satisfy nums[i-1] < nums[i] < nums[i+1];
- 0 otherwise.
Return the total sum of beauty scores for all interior indices.
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 2.
Example 2:
Input: nums = [2,4,6,4]
Output: 1
Explanation: For each index i in the range 1 <= i <= 2:
- The beauty of nums[1] equals 1.
- The beauty of nums[2] equals 0.
Example 3:
Input: nums = [3,2,1]
Output: 0
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 0.
Code
1
2
3