-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 2.7 MB (1…
…2.21%)
- Loading branch information
1 parent
fd6f624
commit 9558884
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
3372-longest-strictly-increasing-or-strictly-decreasing-subarray/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<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> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">2</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<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> | ||
|
||
<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> | ||
|
||
<p>Hence, we return <code>2</code>.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [3,3,3,3]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">1</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p> | ||
|
||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p> | ||
|
||
<p>Hence, we return <code>1</code>.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">3</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, and <code>[1]</code>.</p> | ||
|
||
<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> | ||
|
||
<p>Hence, we return <code>3</code>.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 50</code></li> | ||
<li><code>1 <= nums[i] <= 50</code></li> | ||
</ul> |
19 changes: 19 additions & 0 deletions
19
3372-longest-strictly-increasing-or-strictly-decreasing-subarray/solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
func longestMonotonicSubarray(nums []int) int { | ||
res := 1 | ||
inc := 1 | ||
dec := 1 | ||
for i := 1; i < len(nums); i++ { | ||
if nums[i-1] > nums[i] { | ||
dec++ | ||
inc = 1 | ||
} else if nums[i-1] < nums[i] { | ||
dec = 1 | ||
inc++ | ||
} else { | ||
inc = 1 | ||
dec = 1 | ||
} | ||
res = max(res, inc, dec) | ||
} | ||
return res | ||
} |