Skip to content

Commit 7f45396

Browse files
committed
Feb 3
1 parent bc0a6bd commit 7f45396

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from itertools import pairwise
2+
from typing import List
3+
4+
5+
class Solution:
6+
def longestMonotonicSubarray(self, nums: List[int]) -> int:
7+
curr_incr, curr_decr = 1, 1
8+
max_len = 1
9+
for n1, n2 in pairwise(nums):
10+
curr_incr = (n1 < n2) * curr_incr + 1
11+
curr_decr = (n1 > n2) * curr_decr + 1
12+
max_len = max(max_len, curr_incr, curr_decr)
13+
return max_len
14+
15+
16+
def main():
17+
nums = [1, 4, 3, 3, 2]
18+
assert Solution().longestMonotonicSubarray(nums) == 2
19+
20+
nums = [3, 3, 3, 3]
21+
assert Solution().longestMonotonicSubarray(nums) == 1
22+
23+
nums = [3, 2, 1]
24+
assert Solution().longestMonotonicSubarray(nums) == 3
25+
26+
27+
if __name__ == '__main__':
28+
main()

2025-02-February-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| --- | --- | --- | --- |
77
| February 01 | [3151. Special Array I](https://leetcode.com/problems/special-array-i/) | Easy | Solved |
88
| February 02 | [1752. Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | Easy | Solved |
9-
| February 03 | []() | | |
9+
| February 03 | [3105. Longest Strictly Increasing or Strictly Decreasing Subarray](https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/) | Easy | Solved |
1010
| February 04 | []() | | |
1111
| February 05 | []() | | |
1212
| February 06 | []() | | |
@@ -37,6 +37,6 @@
3737
## Summary
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
40-
| Easy | 2 | 2 | 0 |
40+
| Easy | 3 | 3 | 0 |
4141
| Medium | 0 | 0 | 0 |
4242
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)