Skip to content

Commit e1f1793

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (24.03%), Memory - 17.8 MB (33.50%)
1 parent 8307a79 commit e1f1793

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<p>You are given an array of integers <code>nums</code>. Return <em>the length of the <strong>longest</strong> <span data-keyword="subarray-nonempty">subarray</span> of </em><code>nums</code><em> which is either <strong><span data-keyword="strictly-increasing-array">strictly increasing</span></strong> or <strong><span data-keyword="strictly-decreasing-array">strictly decreasing</span></strong></em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p>The strictly increasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, and <code>[1,4]</code>.</p>
14+
15+
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, <code>[3,2]</code>, and <code>[4,3]</code>.</p>
16+
17+
<p>Hence, we return <code>2</code>.</p>
18+
</div>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">nums = [3,3,3,3]</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
26+
27+
<p><strong>Explanation:</strong></p>
28+
29+
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
30+
31+
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
32+
33+
<p>Hence, we return <code>1</code>.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, and <code>[1]</code>.</p>
46+
47+
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, <code>[1]</code>, <code>[3,2]</code>, <code>[2,1]</code>, and <code>[3,2,1]</code>.</p>
48+
49+
<p>Hence, we return <code>3</code>.</p>
50+
</div>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
57+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
58+
</ul>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Approach 2: Single Iteration
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def longestMonotonicSubarray(self, nums: List[int]) -> int:
8+
inc_len = dec_len = max_len = 1
9+
10+
for pos in range(len(nums) - 1):
11+
if nums[pos + 1] > nums[pos]:
12+
inc_len += 1
13+
dec_len = 1 # Reset decreasing length
14+
15+
elif nums[pos + 1] < nums[pos]:
16+
dec_len += 1
17+
inc_len = 1 # Reset increasing length
18+
19+
else:
20+
inc_len = dec_len = 1 # Reset both
21+
22+
max_len = max(max_len, inc_len, dec_len)
23+
24+
return max_len
25+

0 commit comments

Comments
 (0)