Skip to content

Commit 87db628

Browse files
committed
Create Daily 05-01-24 Another Approach.md
1 parent 5d4d326 commit 87db628

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Today's 05-01-24 [Problem Link](https://leetcode.com/problems/longest-increasing-subsequence/description/?envType=daily-question&envId=2024-01-05)
2+
3+
# Intuition
4+
<!-- Describe your first thoughts on how to solve this problem. -->
5+
- Dynamic Programming Array (gp):
6+
7+
- - My code creates a dynamic programming array (gp) to store the length of the longest increasing subsequence ending at each index i of the array nums.
8+
- Initialization
9+
- - The array gp is initialized with all elements set to 1. This is because each element in nums singularly represents a subsequence of length 1.
10+
- Comparison and Update
11+
- - My code iterates through each element of nums, comparing it with previous elements and updating the gp array based on the increasing subsequence property.
12+
- Final Result
13+
- - The result is the maximum value present in the gp array, which represents the length of the longest increasing subsequence.
14+
15+
16+
# Approach
17+
<!-- Describe your approach to solving the problem. -->
18+
- Dynamic Programming Array (gp) :
19+
- - gp[i] represents the length of the longest increasing subsequence ending at index i.
20+
- - gp[i] will store the numbers form index '0' to 'i-1' which is smaller than nums[i] = that would be the length of increasing sunsequence with nums[i] as largest of that sequence
21+
- Initialization of gp :
22+
- - Initially, all elements in gp are set to 1 because each element in nums forms a subsequence of length 1.
23+
- - filled with '1' as every element singularly represents a subsequence of length '1'
24+
- Iterative Comparison and Update :
25+
- - My code uses a nested loop to iterate through each pair of indices (i, j) where j is less than i.
26+
- - For each pair, if nums[i] > nums[j], it means that nums[i] can be included in the increasing subsequence ending at index i, and the length is updated accordingly
27+
- Maximum Length in gp :
28+
- - The final result is the maximum value present in the gp array, representing the length of the longest increasing subsequence.
29+
---
30+
Have a look at the code , still have any confusion then please let me know in the comments
31+
Keep Solving.:)
32+
# Complexity
33+
- Time complexity : $$O(l^2)$$
34+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
35+
36+
- Space complexity : $$O(l)$$
37+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
38+
$$l$$ : length of array.
39+
# Code
40+
```
41+
class Solution {
42+
public int lengthOfLIS(int[] nums) {
43+
44+
int[] gp = new int[nums.length]; // gp[i] will store the numbers form index '0' to 'i-1' which is smaller than nums[i] = that would be the length of increasing sunsequence with nums[i] as largest of that sequence
45+
Arrays.fill(gp, 1); // fill with '1' as every element singularly represents a subsequence of length '1'
46+
for( int i = 1; i < nums.length; i++){
47+
for( int j = 0; j < i; j++){
48+
if( nums[i] > nums[j]){
49+
gp[i] = Math.max( gp[i] , gp[j] + 1);
50+
}
51+
}
52+
}
53+
return Arrays.stream(gp).max().getAsInt(); // this will give the maximum value present in array
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)