Sum of Beauty in the Array - Problem
You are given a 0-indexed integer array nums. For each index i (where 1 <= i <= nums.length - 2), the beauty of nums[i] equals:
- 2, if
nums[j] < nums[i] < nums[k]for all0 <= j < iand for alli < k <= nums.length - 1. - 1, if
nums[i - 1] < nums[i] < nums[i + 1]and the previous condition is not satisfied. - 0, if none of the previous conditions holds.
Return the sum of beauty of all nums[i] where 1 <= i <= nums.length - 2.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3]
›
Output:
2
💡 Note:
Only middle element is nums[1] = 2. Check beauty 2: all left [1] < 2 < all right [3] ✓. Beauty = 2. Sum = 2.
Example 2 — Multiple Elements
$
Input:
nums = [2,4,6,4]
›
Output:
1
💡 Note:
Middle elements: nums[1] = 4, nums[2] = 6. nums[1]: 2 < 4 < 6 ✓ (beauty 1). nums[2]: 4 < 6 > 4 ✗ (beauty 0). Sum = 1 + 0 = 1.
Example 3 — Maximum Beauty
$
Input:
nums = [3,2,4,2,4]
›
Output:
1
💡 Note:
nums[1] = 2: all left [3] ≥ 2 ✗, neighbors 3 > 2 < 4 ✗ (beauty 0). nums[2] = 4: all left [3,2] < 4 ✓, all right [2,4] ≤ 4 ✗, neighbors 2 < 4 > 2 ✗ (beauty 0). nums[3] = 2: neighbors 4 > 2 < 4 ✓ (beauty 1). Sum = 0 + 0 + 1 = 1.
Constraints
- 3 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code