|
| 1 | +--- |
| 2 | +id: longest-strictly-subarray |
| 3 | +title: Longest Strictly Increasing or Strictly Decreasing Subarray |
| 4 | +sidebar_label: 3105-LongestStrictlySubarray |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Dynamic Programming |
| 8 | +description: Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing. |
| 9 | +sidebar_position: 3105 |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +You are given an array of integers `nums`. Return the length of the longest subarray of `nums` which is either strictly increasing or strictly decreasing. |
| 15 | + |
| 16 | +### Example 1 |
| 17 | + |
| 18 | +- **Input:** `nums = [1,4,3,3,2]` |
| 19 | +- **Output:** `2` |
| 20 | +- **Explanation:** |
| 21 | + - The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4]. |
| 22 | + - The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3]. |
| 23 | + - Hence, we return 2. |
| 24 | + |
| 25 | +### Example 2 |
| 26 | + |
| 27 | +- **Input:** `nums = [3,3,3,3]` |
| 28 | +- **Output:** `1` |
| 29 | +- **Explanation:** |
| 30 | + - The strictly increasing subarrays of nums are [3], [3], [3], and [3]. |
| 31 | + - The strictly decreasing subarrays of nums are [3], [3], [3], and [3]. |
| 32 | + - Hence, we return 1. |
| 33 | + |
| 34 | +### Example 3 |
| 35 | + |
| 36 | +- **Input:** `nums = [3,2,1]` |
| 37 | +- **Output:** `3` |
| 38 | +- **Explanation:** |
| 39 | + - The strictly increasing subarrays of nums are [3], [2], and [1]. |
| 40 | + - The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1]. |
| 41 | + - Hence, we return 3. |
| 42 | + |
| 43 | +### Constraints |
| 44 | + |
| 45 | +- `1 <= nums.length <= 50` |
| 46 | +- `1 <= nums[i] <= 50` |
| 47 | + |
| 48 | +## Approach |
| 49 | + |
| 50 | +To solve this problem, we can use a dynamic programming approach: |
| 51 | + |
| 52 | +1. **Dynamic Programming Array**: |
| 53 | + - Use two arrays `inc` and `dec` to store the lengths of the longest strictly increasing and strictly decreasing subarrays ending at each index `i` in `nums`. |
| 54 | + - Traverse through the array from left to right to populate `inc`. |
| 55 | + - Traverse through the array from right to left to populate `dec`. |
| 56 | + - The maximum value from `inc` and `dec` arrays will give us the length of the longest subarray that is either strictly increasing or strictly decreasing. |
| 57 | + |
| 58 | +### Solution Code |
| 59 | + |
| 60 | +#### Python |
| 61 | + |
| 62 | +```python |
| 63 | +class Solution: |
| 64 | + def longestMonotonicSubarray(self, nums: List[int]) -> int: |
| 65 | + n = len(nums) |
| 66 | + if n <= 1: |
| 67 | + return n |
| 68 | + |
| 69 | + inc = [1] * n # Length of longest strictly increasing subarray ending at index i |
| 70 | + dec = [1] * n # Length of longest strictly decreasing subarray ending at index i |
| 71 | + |
| 72 | + # Fill inc array |
| 73 | + for i in range(1, n): |
| 74 | + if nums[i] > nums[i - 1]: |
| 75 | + inc[i] = inc[i - 1] + 1 |
| 76 | + |
| 77 | + # Fill dec array |
| 78 | + for i in range(n - 2, -1, -1): |
| 79 | + if nums[i] > nums[i + 1]: |
| 80 | + dec[i] = dec[i + 1] + 1 |
| 81 | + |
| 82 | + # Find the maximum length of strictly increasing or strictly decreasing subarray |
| 83 | + max_len = 1 # At least a single element subarray is valid |
| 84 | + for i in range(n): |
| 85 | + max_len = max(max_len, inc[i], dec[i]) |
| 86 | + |
| 87 | + return max_len |
| 88 | +``` |
| 89 | + |
| 90 | +#### C++ |
| 91 | + |
| 92 | +```c++ |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + int longestMonotonicSubarray(vector<int>& nums) { |
| 96 | + int n = nums.size(); |
| 97 | + if (n <= 1) { |
| 98 | + return n; |
| 99 | + } |
| 100 | + |
| 101 | + vector<int> inc(n, 1); // Length of longest strictly increasing subarray ending at index i |
| 102 | + vector<int> dec(n, 1); // Length of longest strictly decreasing subarray ending at index i |
| 103 | + |
| 104 | + // Fill inc array |
| 105 | + for (int i = 1; i < n; i++) { |
| 106 | + if (nums[i] > nums[i - 1]) { |
| 107 | + inc[i] = inc[i - 1] + 1; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Fill dec array |
| 112 | + for (int i = n - 2; i >= 0; i--) { |
| 113 | + if (nums[i] > nums[i + 1]) { |
| 114 | + dec[i] = dec[i + 1] + 1; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Find the maximum length of strictly increasing or strictly decreasing subarray |
| 119 | + int maxLen = 1; // At least a single element subarray is valid |
| 120 | + for (int i = 0; i < n; i++) { |
| 121 | + maxLen = max(maxLen, max(inc[i], dec[i])); |
| 122 | + } |
| 123 | + |
| 124 | + return maxLen; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +``` |
| 129 | +
|
| 130 | +#### Java |
| 131 | +
|
| 132 | +```java |
| 133 | +class Solution { |
| 134 | + public int longestMonotonicSubarray(int[] nums) { |
| 135 | + int n = nums.length; |
| 136 | + if (n <= 1) { |
| 137 | + return n; |
| 138 | + } |
| 139 | + |
| 140 | + int[] inc = new int[n]; // Length of longest strictly increasing subarray ending at index i |
| 141 | + int[] dec = new int[n]; // Length of longest strictly decreasing subarray ending at index i |
| 142 | + |
| 143 | + // Fill inc array |
| 144 | + Arrays.fill(inc, 1); |
| 145 | + for (int i = 1; i < n; i++) { |
| 146 | + if (nums[i] > nums[i - 1]) { |
| 147 | + inc[i] = inc[i - 1] + 1; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Fill dec array |
| 152 | + Arrays.fill(dec, 1); |
| 153 | + for (int i = n - 2; i >= 0; i--) { |
| 154 | + if (nums[i] > nums[i + 1]) { |
| 155 | + dec[i] = dec[i + 1] + 1; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Find the maximum length of strictly increasing or strictly decreasing subarray |
| 160 | + int maxLen = 1; // At least a single element subarray is valid |
| 161 | + for (int i = 0; i < n; i++) { |
| 162 | + maxLen = Math.max(maxLen, Math.max(inc[i], dec[i])); |
| 163 | + } |
| 164 | + |
| 165 | + return maxLen; |
| 166 | + } |
| 167 | +} |
| 168 | +
|
| 169 | +``` |
| 170 | + |
| 171 | +### Comclusion |
| 172 | +The above solutions use dynamic programming to find the length of the longest subarray in nums that |
| 173 | +is either strictly increasing or strictly decreasing. They ensure efficient computation within the |
| 174 | +given constraints, providing robust solutions across different inputs. |
0 commit comments