Skip to content
LC-2765 Easy LeetCode

2765. Longest Alternating Subarray

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 34% Topics: Array, Enumeration
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# array
class Solution(object):
    def alternatingSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result = l = -1
        for i in xrange(len(nums)-1):
            if l != -1 and nums[i-1] == nums[i+1]:
                l += 1
            else:
                l = 2 if nums[i+1]-nums[i] == 1 else -1
            result = max(result, l)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT