aboutsummaryrefslogtreecommitdiff
path: root/long_strictly_incr_decr_subarr.py
blob: 108392e51dd2c2284714b012b065c0c5499a799d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 3105. Longest Strictly Increasing or Strictly Decreasing Subarray

"""
you are given an array of integers 'nums'. return the legnth of the longest
subarray of 'nums' which is either strictly increasing or strictly
decreasing.
"""


class Solution(object):
    def longestMonotonicSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n, ans, inc, dec = len(nums), 1, 1, 1
        for i in range(1, n):
            a, b = nums[i] > nums[i - 1], nums[i] < nums[i - 1]
            inc = a * inc + 1
            dec = b * dec + 1
            ans = max(ans, dec, inc)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.longestMonotonicSubarray(nums=[1, 4, 3, 3, 2]))
    print(obj.longestMonotonicSubarray(nums=[3, 3, 3, 3]))
    print(obj.longestMonotonicSubarray(nums=[3, 2, 1]))