diff options
Diffstat (limited to 'long_strictly_incr_decr_subarr.py')
-rw-r--r-- | long_strictly_incr_decr_subarr.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/long_strictly_incr_decr_subarr.py b/long_strictly_incr_decr_subarr.py new file mode 100644 index 0000000..108392e --- /dev/null +++ b/long_strictly_incr_decr_subarr.py @@ -0,0 +1,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])) |