3105. Longest Strictly Increasing or Strictly Decreasing Subarray LeetCode Solution
In this guide, you will get 3105. Longest Strictly Increasing or Strictly Decreasing Subarray LeetCode Solution with the best time and space complexity. The solution to Longest Strictly Increasing or Strictly Decreasing Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Longest Strictly Increasing or Strictly Decreasing Subarray solution in C++
- Longest Strictly Increasing or Strictly Decreasing Subarray solution in Java
- Longest Strictly Increasing or Strictly Decreasing Subarray solution in Python
- Additional Resources
Problem Statement of Longest Strictly Increasing or Strictly Decreasing Subarray
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.
Example 1:
Input: nums = [1,4,3,3,2]
Output: 2
Explanation:
The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].
The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].
Hence, we return 2.
Example 2:
Input: nums = [3,3,3,3]
Output: 1
Explanation:
The strictly increasing subarrays of nums are [3], [3], [3], and [3].
The strictly decreasing subarrays of nums are [3], [3], [3], and [3].
Hence, we return 1.
Example 3:
Input: nums = [3,2,1]
Output: 3
Explanation:
The strictly increasing subarrays of nums are [3], [2], and [1].
The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].
Hence, we return 3.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3105. Longest Strictly Increasing or Strictly Decreasing Subarray LeetCode Solution in C++
class Solution {
public:
// Similar to 978. Longest Turbulent Subarray
int longestMonotonicSubarray(vector<int>& nums) {
int ans = 1;
int increasing = 1;
int decreasing = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i - 1]) {
increasing += 1;
decreasing = 1;
} else if (nums[i] < nums[i - 1]) {
decreasing += 1;
increasing = 1;
} else {
increasing = 1;
decreasing = 1;
}
ans = max({ans, increasing, decreasing});
}
return ans;
}
};
/* code provided by PROGIEZ */
3105. Longest Strictly Increasing or Strictly Decreasing Subarray LeetCode Solution in Java
class Solution {
// Similar to 978. Longest Turbulent Subarray
public int longestMonotonicSubarray(int[] nums) {
int ans = 1;
int increasing = 1;
int decreasing = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] > nums[i - 1]) {
increasing += 1;
decreasing = 1;
} else if (nums[i] < nums[i - 1]) {
decreasing += 1;
increasing = 1;
} else {
increasing = 1;
decreasing = 1;
}
ans = Math.max(ans, Math.max(increasing, decreasing));
}
return ans;
}
}
// code provided by PROGIEZ
3105. Longest Strictly Increasing or Strictly Decreasing Subarray LeetCode Solution in Python
class Solution:
# Similar to 978. Longest Turbulent Subarray
def longestMonotonicSubarray(self, nums: list[int]) -> int:
ans = 1
increasing = 1
decreasing = 1
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
increasing += 1
decreasing = 1
elif nums[i] < nums[i - 1]:
decreasing += 1
increasing = 1
else:
increasing = 1
decreasing = 1
ans = max(ans, increasing, decreasing)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.