Problem statement

https://binarysearch.com/problems/Longest-Sign-Alternating-Subsequence/

Solution

We can just use groupby to find number of alternating groups of signs.

Complexity

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

Code

class Solution:
    def solve(self, nums):
        ans = 0
        for x, y in groupby([x < 0 for x in nums]):
            ans += 1
        return ans