Problem statement

https://binarysearch.com/problems/Changing-Directions/

Solution

We just need to find number of local minimums and maximums.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, A):
        n, ans = len(A), 0
        for i in range(1, n-1):
            if A[i-1] < A[i] > A[i+1] or A[i-1] > A[i] < A[i+1]:
                ans += 1
        return ans