Given an unsorted array of integers nums
, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l
and r
(l < r
) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
and for each l <= i < r
, nums[i] < nums[i + 1]
.
Example 1:
Input: nums = [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3. Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
Example 2:
Input: nums = [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the longest increasing sequence involves checking every possible subsequence within the given sequence. We explore all combinations, comparing each to find the longest one that strictly increases as you go from left to right. Essentially, we leave no stone unturned.
Here's how the algorithm would work step-by-step:
def find_longest_increasing_subsequence(number_list):
longest_subsequence_length = 0
for starting_index in range(len(number_list)):
current_subsequence_length = 0
current_subsequence = []
# Start a subsequence with the current number
current_subsequence.append(number_list[starting_index])
current_subsequence_length += 1
for next_index in range(starting_index + 1, len(number_list)):
# Check if the next number extends subsequence
if number_list[next_index] > current_subsequence[-1]:
current_subsequence.append(number_list[next_index])
current_subsequence_length += 1
# Compare and update longest subsequence
if current_subsequence_length > longest_subsequence_length:
longest_subsequence_length = current_subsequence_length
return longest_subsequence_length
The most efficient way to find the longest continuous increasing sequence is to scan the numbers once, keeping track of how long the current streak is. We only need to update our answer when we find a longer streak, skipping all possible sub-sequences.
Here's how the algorithm would work step-by-step:
def find_longest_increasing_subsequence(numbers):
if not numbers:
return 0
longest_streak_length = 1
current_streak_length = 1
for i in range(1, len(numbers)):
# Check if current number continues sequence.
if numbers[i] > numbers[i - 1]:
current_streak_length += 1
# Update longest streak if needed.
if current_streak_length > longest_streak_length:
longest_streak_length = current_streak_length
# Reset streak if sequence is broken.
else:
current_streak_length = 1
return longest_streak_length
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately as there is no subsequence. |
Array with a single element | Return 1 immediately as a single element is a subsequence of length 1. |
Array with all elements in descending order | The algorithm should still return 1 because a single element is an increasing subsequence. |
Array with all elements being identical | The algorithm should return 1, as the longest increasing subsequence consists of any single element. |
Array with a very long continuous increasing subsequence | The algorithm should handle this efficiently (O(n) time complexity) without stack overflow. |
Array with negative numbers and/or zero | The algorithm should correctly handle negative and zero values as they can be part of an increasing sequence. |
Array contains Integer.MAX_VALUE and/or Integer.MIN_VALUE | The algorithm should handle these boundary values without causing integer overflow issues during comparison. |
Very large input array size nearing memory limits | Ensure the algorithm doesn't allocate excessive memory that could lead to out-of-memory errors. |