πDay 1. Longest Increasing Subsequence π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given an array arr[] of non-negative integers, the task is to find the length of the Longest Strictly Increasing Subsequence (LIS).
A subsequence is strictly increasing if each element in the subsequence is strictly less than the next element.
π Example Walkthrough:
Example 1:
Input:
arr[] = [5, 8, 3, 7, 9, 1]
Output:
3
Explanation:
The longest strictly increasing subsequence could be [5, 7, 9], which has a length of 3.
Example 2:
Input:
arr[] = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
Output:
6
Explanation:
One of the possible longest strictly increasing subsequences is [0, 2, 6, 9, 13, 15], which has a length of 6.
Example 3:
Input:
arr[] = [3, 10, 2, 1, 20]
Output:
3
Explanation:
The longest strictly increasing subsequence could be [3, 10, 20], which has a length of 3.
Constraints:
$(1 \leq arr.size() \leq 10^3)$
$(0 \leq arr[i] \leq 10^6)$
π― My Approach:
1οΈβ£ Optimized Binary Search Approach
Algorithm Steps:
Iterate through each element of the array.
Maintain a list
answhich tracks the smallest possible tail for increasing subsequences of different lengths.For each element, find its correct position in
ansusinglower_bound(binary search).If the element can extend the current LIS, append it to
ans, else replace the element at the correct position inansto keepansoptimized.The size of
ansat the end represents the length of the longest increasing subsequence.
Step-by-step Process
Initialize an empty list
lis.For each element
numin the array:Use binary search (
lower_bound) to find the position wherenumcan replace or extend thelislist.If
numis larger than all elements inlis, append it.Otherwise, replace the existing element at the correct position with
num, maintaining the smallest possible value for subsequences of that length.
Return the size of
lis, which is the length of the Longest Increasing Subsequence.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N log N), where
Nis the size of the array, as we perform binary search for each element.Expected Auxiliary Space Complexity: O(N), for storing the
ansarray.
π Solution Code
Code (C++)
class Solution {
public:
int lis(std::vector<int>& arr) {
std::vector<int> ans;
for (int num : arr) {
auto it = std::lower_bound(ans.begin(), ans.end(), num);
if (it == ans.end()) ans.push_back(num);
else *it = num;
}
return ans.size();
}
};Code (Java)
class Solution {
public int lis(int[] arr) {
ArrayList<Integer> ans = new ArrayList<>();
for (int num : arr) {
int idx = Collections.binarySearch(ans, num);
if (idx < 0) idx = -idx - 1;
if (idx == ans.size()) ans.add(num);
else ans.set(idx, num);
}
return ans.size();
}
}Code (Python)
import bisect
class Solution:
def lis(self, arr):
ans = []
for num in arr:
idx = bisect.bisect_left(ans, num)
if idx == len(ans):
ans.append(num)
else:
ans[idx] = num
return len(ans)π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated