Skip to content

Commit e707446

Browse files
committed
Update README.md
1 parent 87db628 commit e707446

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,60 @@ class Solution {
6868
}
6969
}
7070
```
71+
72+
# Alternate Approach
73+
74+
# Intuition
75+
<!-- Describe your first thoughts on how to solve this problem. -->
76+
- Dynamic Programming Array (gp):
77+
78+
- - 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.
79+
- Initialization
80+
- - 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.
81+
- Comparison and Update
82+
- - My code iterates through each element of nums, comparing it with previous elements and updating the gp array based on the increasing subsequence property.
83+
- Final Result
84+
- - The result is the maximum value present in the gp array, which represents the length of the longest increasing subsequence.
85+
86+
87+
# Approach
88+
<!-- Describe your approach to solving the problem. -->
89+
- Dynamic Programming Array (gp) :
90+
- - gp[i] represents the length of the longest increasing subsequence ending at index i.
91+
- - 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
92+
- Initialization of gp :
93+
- - Initially, all elements in gp are set to 1 because each element in nums forms a subsequence of length 1.
94+
- - filled with '1' as every element singularly represents a subsequence of length '1'
95+
- Iterative Comparison and Update :
96+
- - My code uses a nested loop to iterate through each pair of indices (i, j) where j is less than i.
97+
- - 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
98+
- Maximum Length in gp :
99+
- - The final result is the maximum value present in the gp array, representing the length of the longest increasing subsequence.
100+
---
101+
Have a look at the code , still have any confusion then please let me know in the comments
102+
Keep Solving.:)
103+
# Complexity
104+
- Time complexity : $$O(l^2)$$
105+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
106+
107+
- Space complexity : $$O(l)$$
108+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
109+
$$l$$ : length of array.
110+
# Code
111+
```
112+
class Solution {
113+
public int lengthOfLIS(int[] nums) {
114+
115+
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
116+
Arrays.fill(gp, 1); // fill with '1' as every element singularly represents a subsequence of length '1'
117+
for( int i = 1; i < nums.length; i++){
118+
for( int j = 0; j < i; j++){
119+
if( nums[i] > nums[j]){
120+
gp[i] = Math.max( gp[i] , gp[j] + 1);
121+
}
122+
}
123+
}
124+
return Arrays.stream(gp).max().getAsInt(); // this will give the maximum value present in array
125+
}
126+
}
127+
```

0 commit comments

Comments
 (0)