Skip to content
LC-2210 Easy LeetCode

2210. Count Hills and Valleys in an Array

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 62% Topics: Array
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# simulation, array
class Solution(object):
    def countHillValley(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result, inc = 0, -1
        for i in xrange(len(nums)-1):
            if nums[i] < nums[i+1]:
                result += int(inc == 0)
                inc = 1
            elif nums[i] > nums[i+1]:
                result += int(inc == 1)
                inc = 0
        return result

Solution from kamyu104/LeetCode-Solutions · MIT