You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an integer array nums, return the length of the longest strictly
5
+
increasing subsequence.
3
6
4
-
//The longest increasing subsequence is described as a subsequence of an array where:
5
-
// All elements of the subsequence are in increasing order.
6
-
// This subsequence itself is of the longest length possible.
7
+
The longest increasing subsequence is described as a subsequence of an array
8
+
where: All elements of the subsequence are in increasing order. This subsequence
9
+
itself is of the longest length possible.
7
10
8
-
//For solving this problem we have Three Approaches :-
11
+
For solving this problem we have Three Approaches :-
9
12
10
-
// Approach 1 :- Using Brute Force
11
-
// The first approach that came to your mind is the Brute Force approach where we generate all subsequences and then manually filter the subsequences whose elements come in increasing order and then return the longest such subsequence.
12
-
// Time Complexity :- O(2^n)
13
-
// It's time complexity is exponential. Therefore we will try some other approaches.
13
+
Approach 1 :- Using Brute Force
14
+
The first approach that came to your mind is the Brute Force approach where we
15
+
generate all subsequences and then manually filter the subsequences whose
16
+
elements come in increasing order and then return the longest such subsequence.
17
+
Time Complexity :- O(2^n)
18
+
It's time complexity is exponential. Therefore we will try some other
19
+
approaches.
14
20
15
-
// Approach 2 :- Using Dynamic Programming
16
-
// To generate all subsequences we will use recursion and in the recursive logic we will figure out a way to solve this problem.
17
-
// Recursive Logic to solve this problem:-
18
-
// 1. We only consider the element in the subsequence if the element is grater then the last element present in the subsequence
19
-
// 2. When we consider the element we will increase the length of subsequence by 1
20
-
// Time Complexity: O(N*N)
21
-
// Space Complexity: O(N*N) + O(N)
21
+
Approach 2 :- Using Dynamic Programming
22
+
To generate all subsequences we will use recursion and in the recursive logic we
23
+
will figure out a way to solve this problem. Recursive Logic to solve this
24
+
problem:-
25
+
1. We only consider the element in the subsequence if the element is grater then
26
+
the last element present in the subsequence
27
+
2. When we consider the element we will increase the length of subsequence by 1
28
+
Time Complexity: O(N*N)
29
+
Space Complexity: O(N*N) + O(N)
22
30
23
-
// This approach is better then the previous Brute Force approach so, we can consider this approach.
31
+
This approach is better then the previous Brute Force approach so, we can
32
+
consider this approach.
24
33
25
-
//But when the Constraints for the problem is very larger then this approach fails
34
+
But when the Constraints for the problem is very larger then this approach fails
26
35
27
-
// Approach 3 :- Using Binary Search
28
-
// Other approaches use additional space to create a new subsequence Array. Instead, this solution uses the existing nums Array to build the subsequence array. We can do this because the length of the subsequence array will never be longer than the current index.
36
+
Approach 3 :- Using Binary Search
37
+
Other approaches use additional space to create a new subsequence Array.
38
+
Instead, this solution uses the existing nums Array to build the subsequence
39
+
array. We can do this because the length of the subsequence array will never be
40
+
longer than the current index.
29
41
30
-
//Time complexity: O(n∗log(n))
31
-
//Space complexity: O(1)
42
+
Time complexity: O(n∗log(n))
43
+
Space complexity: O(1)
32
44
33
-
//This approach consider Most optimal Approach for solving this problem
45
+
This approach consider Most optimal Approach for solving this problem
0 commit comments