Welcome to Subscribe On Youtube
4088. Difference Between Peaks and Valleys
Description
You are given an integer array nums of length n.
An index i is called a peak if all of the following conditions hold:
0 < i < n - 1nums[i] > nums[i - 1]nums[i] > nums[i + 1]
An index i is called a valley if all of the following conditions hold:
0 < i < n - 1nums[i] < nums[i - 1]nums[i] < nums[i + 1]
Return an integer denoting the difference between the sum of all peak values and the sum of all valley values.
Example 1:
Input: nums = [1,5,2,4,1,3,0]
Output: 9
Explanation:
- The peak values are
nums[1] = 5,nums[3] = 4, andnums[5] = 3. - The valley values are
nums[2] = 2andnums[4] = 1. - The difference between the sum of the peak values and the sum of the valley values is
(5 + 4 + 3) - (2 + 1) = 9. Hence, the answer is 9.
Example 2:
Input: nums = [-5,-6,-1,-2,5,3,2]
Output: 12
Explanation:
- The peak values are
nums[2] = -1andnums[4] = 5. - The valley values are
nums[1] = -6andnums[3] = -2. - The difference between the sum of the peak values and the sum of the valley values is
(-1 + 5) - (-6 + (-2)) = 12. Hence, the answer is 12.
Example 3:
Input: nums = [6,5,2,5,5,3,6]
Output: -5
Explanation:
- There are no peak values in the array.
- The valley values are
nums[2] = 2andnums[5] = 3. - The difference between the sum of the peak values and the sum of the valley values is
(0) - (2 + 3) = -5. Hence, the answer is -5.
Constraints:
3 <= n == nums.length <= 100-100 <= nums[i] <= 100
Solutions
Solution 1: Single Pass
Thinking
$n$ is at most $100$, so comparing each interior index with its two neighbors is enough. A peak or a valley depends only on three adjacent positions, so the difference can be accumulated during the scan.
Equal neighbors satisfy neither definition. A position contributes a positive value only when it is strictly greater than both sides, and a negative value only when it is strictly smaller than both sides.
The required difference is the sum of these contributions. One left-to-right scan adds $\textit{nums}[i]$ at a peak and subtracts $\textit{nums}[i]$ at a valley.
Index $i$ is a peak exactly when $0<i<n-1$ and $\textit{nums}[i]$ is strictly greater than both $\textit{nums}[i-1]$ and $\textit{nums}[i+1]$. A valley uses the same bounds with both comparisons reversed. Endpoints are never included.
Scan $i$ from $1$ to $n-2$. Add the current value when it is greater than both neighbors, and subtract it when it is smaller than both neighbors. Every other position leaves the answer unchanged.
The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the array.
-
class Solution { public int peakValleyDifference(int[] nums) { int ans = 0; for (int i = 1; i < nums.length - 1; i++) { if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { ans += nums[i]; } else if (nums[i - 1] > nums[i] && nums[i] < nums[i + 1]) { ans -= nums[i]; } } return ans; } } -
class Solution { public: int peakValleyDifference(vector<int>& nums) { int ans = 0; for (int i = 1; i < nums.size() - 1; i++) { if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { ans += nums[i]; } else if (nums[i - 1] > nums[i] && nums[i] < nums[i + 1]) { ans -= nums[i]; } } return ans; } }; -
class Solution: def peakValleyDifference(self, nums: list[int]) -> int: ans = 0 for i in range(1, len(nums) - 1): if nums[i - 1] < nums[i] > nums[i + 1]: ans += nums[i] elif nums[i - 1] > nums[i] < nums[i + 1]: ans -= nums[i] return ans -
func peakValleyDifference(nums []int) int { ans := 0 for i := 1; i < len(nums)-1; i++ { if nums[i-1] < nums[i] && nums[i] > nums[i+1] { ans += nums[i] } else if nums[i-1] > nums[i] && nums[i] < nums[i+1] { ans -= nums[i] } } return ans } -
function peakValleyDifference(nums: number[]): number { let ans = 0; for (let i = 1; i < nums.length - 1; i++) { if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { ans += nums[i]; } else if (nums[i - 1] > nums[i] && nums[i] < nums[i + 1]) { ans -= nums[i]; } } return ans; }