|
67 | 67 |
|
68 | 68 | ## Solutions
|
69 | 69 |
|
70 |
| -### Solution 1 |
| 70 | +### Solution 1: Two Passes |
| 71 | + |
| 72 | +We first perform a pass to find the length of the longest strictly increasing subarray, and update the answer. Then we perform another pass to find the length of the longest strictly decreasing subarray, and update the answer again. |
| 73 | + |
| 74 | +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. |
71 | 75 |
|
72 | 76 | <!-- tabs:start -->
|
73 | 77 |
|
74 | 78 | ```python
|
75 |
| - |
| 79 | +class Solution: |
| 80 | + def longestMonotonicSubarray(self, nums: List[int]) -> int: |
| 81 | + ans = t = 1 |
| 82 | + for i, x in enumerate(nums[1:]): |
| 83 | + if nums[i] < x: |
| 84 | + t += 1 |
| 85 | + ans = max(ans, t) |
| 86 | + else: |
| 87 | + t = 1 |
| 88 | + t = 1 |
| 89 | + for i, x in enumerate(nums[1:]): |
| 90 | + if nums[i] > x: |
| 91 | + t += 1 |
| 92 | + ans = max(ans, t) |
| 93 | + else: |
| 94 | + t = 1 |
| 95 | + return ans |
76 | 96 | ```
|
77 | 97 |
|
78 | 98 | ```java
|
79 |
| - |
| 99 | +class Solution { |
| 100 | + public int longestMonotonicSubarray(int[] nums) { |
| 101 | + int ans = 1; |
| 102 | + for (int i = 1, t = 1; i < nums.length; ++i) { |
| 103 | + if (nums[i - 1] < nums[i]) { |
| 104 | + ans = Math.max(ans, ++t); |
| 105 | + } else { |
| 106 | + t = 1; |
| 107 | + } |
| 108 | + } |
| 109 | + for (int i = 1, t = 1; i < nums.length; ++i) { |
| 110 | + if (nums[i - 1] > nums[i]) { |
| 111 | + ans = Math.max(ans, ++t); |
| 112 | + } else { |
| 113 | + t = 1; |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +} |
80 | 119 | ```
|
81 | 120 |
|
82 | 121 | ```cpp
|
83 |
| - |
| 122 | +class Solution { |
| 123 | +public: |
| 124 | + int longestMonotonicSubarray(vector<int>& nums) { |
| 125 | + int ans = 1; |
| 126 | + for (int i = 1, t = 1; i < nums.size(); ++i) { |
| 127 | + if (nums[i - 1] < nums[i]) { |
| 128 | + ans = max(ans, ++t); |
| 129 | + } else { |
| 130 | + t = 1; |
| 131 | + } |
| 132 | + } |
| 133 | + for (int i = 1, t = 1; i < nums.size(); ++i) { |
| 134 | + if (nums[i - 1] > nums[i]) { |
| 135 | + ans = max(ans, ++t); |
| 136 | + } else { |
| 137 | + t = 1; |
| 138 | + } |
| 139 | + } |
| 140 | + return ans; |
| 141 | + } |
| 142 | +}; |
84 | 143 | ```
|
85 | 144 |
|
86 | 145 | ```go
|
| 146 | +func longestMonotonicSubarray(nums []int) int { |
| 147 | + ans := 1 |
| 148 | + t := 1 |
| 149 | + for i, x := range nums[1:] { |
| 150 | + if nums[i] < x { |
| 151 | + t++ |
| 152 | + ans = max(ans, t) |
| 153 | + } else { |
| 154 | + t = 1 |
| 155 | + } |
| 156 | + } |
| 157 | + t = 1 |
| 158 | + for i, x := range nums[1:] { |
| 159 | + if nums[i] > x { |
| 160 | + t++ |
| 161 | + ans = max(ans, t) |
| 162 | + } else { |
| 163 | + t = 1 |
| 164 | + } |
| 165 | + } |
| 166 | + return ans |
| 167 | +} |
| 168 | +``` |
87 | 169 |
|
| 170 | +```ts |
| 171 | +function longestMonotonicSubarray(nums: number[]): number { |
| 172 | + let ans = 1; |
| 173 | + for (let i = 1, t = 1; i < nums.length; ++i) { |
| 174 | + if (nums[i - 1] < nums[i]) { |
| 175 | + ans = Math.max(ans, ++t); |
| 176 | + } else { |
| 177 | + t = 1; |
| 178 | + } |
| 179 | + } |
| 180 | + for (let i = 1, t = 1; i < nums.length; ++i) { |
| 181 | + if (nums[i - 1] > nums[i]) { |
| 182 | + ans = Math.max(ans, ++t); |
| 183 | + } else { |
| 184 | + t = 1; |
| 185 | + } |
| 186 | + } |
| 187 | + return ans; |
| 188 | +} |
88 | 189 | ```
|
89 | 190 |
|
90 | 191 | <!-- tabs:end -->
|
|
0 commit comments