Skip to content

Commit 1cd3b88

Browse files
committed
3372-LONGEST-STRICTLY-INCREASING-OR-STRICTLY-DECREASING-SUBARRAY Stats: Time: 0 ms (100%), Space: 27.9 MB (27.06%) - LeetHub
1 parent 88c2088 commit 1cd3b88

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int longestMonotonicSubarray(vector<int>& nums) {
4+
int cnt = 0;
5+
6+
int n = nums.size();
7+
int lcl_cnt = 0;
8+
int lcl_cnt1 = 0;
9+
for (int i = 0; i < n - 1; i++) {
10+
if (nums[i] < nums[i + 1]) {
11+
lcl_cnt++;
12+
lcl_cnt1 = 0;
13+
} else if (nums[i] > nums[i + 1]) {
14+
lcl_cnt1++;
15+
lcl_cnt = 0;
16+
} else {
17+
lcl_cnt = 0;
18+
lcl_cnt1 = 0;
19+
};
20+
21+
cnt = max(cnt, max(lcl_cnt, lcl_cnt1));
22+
}
23+
24+
return cnt+1;
25+
}
26+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<h2><a href="https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray">3372. Longest Strictly Increasing or Strictly Decreasing Subarray</a></h2><h3>Easy</h3><hr><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>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ this is readme. here i will make
2727
| [2766-find-the-prefix-common-array-of-two-arrays](https://github.com/TheVinaySagar/Leetcode/tree/master/2766-find-the-prefix-common-array-of-two-arrays) |
2828
| [2792-neighboring-bitwise-xor](https://github.com/TheVinaySagar/Leetcode/tree/master/2792-neighboring-bitwise-xor) |
2929
| [3309-count-prefix-and-suffix-pairs-i](https://github.com/TheVinaySagar/Leetcode/tree/master/3309-count-prefix-and-suffix-pairs-i) |
30+
| [3372-longest-strictly-increasing-or-strictly-decreasing-subarray](https://github.com/TheVinaySagar/Leetcode/tree/master/3372-longest-strictly-increasing-or-strictly-decreasing-subarray) |
3031
| [3429-special-array-i](https://github.com/TheVinaySagar/Leetcode/tree/master/3429-special-array-i) |
3132
## String
3233
| |

stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"leetcode":{"shas":{"README.md":{"":"b468eee3656e89be96b977ef83c3fc49e5d33c4b"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"2c88a80dfe972059ae86c77fdc3f00c62982cd4a","difficulty":"medium"},"1878-check-if-array-is-sorted-and-rotated":{"sha":"cc065e516e58d8498b50ea24f3b1a51e211b6010","difficulty":"easy"},"0733-flood-fill":{"sha":"","difficulty":"easy"}},"solved":41,"easy":10,"medium":24,"hard":7}}
1+
{"leetcode":{"shas":{"README.md":{"":"c2404a790a6e79b6f906ed2dd544f18dda7261dc"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"2c88a80dfe972059ae86c77fdc3f00c62982cd4a","difficulty":"medium"},"1878-check-if-array-is-sorted-and-rotated":{"sha":"cc065e516e58d8498b50ea24f3b1a51e211b6010","difficulty":"easy"},"0733-flood-fill":{"sha":"88c20887f3a61a64055ccee8d80b855da3be9494","difficulty":"easy"},"3372-longest-strictly-increasing-or-strictly-decreasing-subarray":{"sha":"","difficulty":"easy"}},"solved":42,"easy":11,"medium":24,"hard":7}}

0 commit comments

Comments
 (0)