Given an integer array nums, return the length of the longest strictly increasing subsequence.
Example 1:
Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:
Input: nums = [0,1,0,3,2,3] Output: 4
Example 3:
Input: nums = [7,7,7,7,7,7,7] Output: 1
Constraints:
1 <= nums.length <= 2500-104 <= nums[i] <= 104Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
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 sequence of numbers that always go up involves looking at every possible combination of numbers from the list. By testing every single path, we can guarantee we find the longest one, even if it takes a lot of effort.
Here's how the algorithm would work step-by-step:
def length_of_longest_increasing_subsequence(sequence_numbers):
def explore_all_combinations(current_index, previous_value):
# We stop searching when we reach the end of the input list
if current_index == len(sequence_numbers):
return 0
skip_current_choice = explore_all_combinations(current_index + 1, previous_value)
take_current_choice = 0
# We can only include the current number if it is strictly greater than the previous one
if sequence_numbers[current_index] > previous_value:
take_current_choice = 1 + explore_all_combinations(current_index + 1, sequence_numbers[current_index])
# We return the maximum length found between skipping or taking the current number
return max(skip_current_choice, take_current_choice)
# We initiate the recursive search using a sentinel value smaller than any possible number
return explore_all_combinations(0, float('-inf'))Instead of checking every single possible sequence, the optimal approach maintains a running collection of growing piles of numbers. As you look through the items one by one, you strategically place each number into the most logical pile so that you always keep your options for future growth as open as possible.
Here's how the algorithm would work step-by-step:
import bisect
def length_of_lis(sequence_numbers):
active_piles_tops = []
# We place each number in the best pile to keep future growth options open.
for current_number in sequence_numbers:
insertion_index = bisect.bisect_left(active_piles_tops, current_number)
# A larger number extends our longest sequence by creating a new pile.
if insertion_index == len(active_piles_tops):
active_piles_tops.append(current_number)
# A smaller number strengthens an existing pile to help future elements.
else:
active_piles_tops[insertion_index] = current_number
return len(active_piles_tops)| Case | How to Handle |
|---|---|
| Empty or null input array | Check for null or empty input at the very beginning and return a length of zero. |
| Single element array | The algorithm naturally processes the single element and returns a length of one. |
| Array with all identical elements | Since the problem requires strictly increasing subsequences, duplicate values will be ignored and the length returned will be one. |
| Array sorted in strictly decreasing order | No element can be appended to an increasing sequence, so the maximum length returned will be one. |
| Array with negative numbers and zeros | Standard comparison operators handle negative values and zeros correctly without requiring offset adjustments. |
| Maximum size input array up to constraints | Using the O(n log n) patience sorting approach with binary search ensures efficient scaling well within time limits. |
| Array with extreme boundary integer values | Standard integer comparisons handle extreme positive and negative values correctly without risk of overflow. |
| Multiple valid increasing subsequences of the same maximum length | The algorithm correctly tracks the length regardless of which alternative path achieves the maximum. |