Skip to content

Commit 4cbf92f

Browse files
committed
solve longest increasing subsequence
1 parent 3ee9846 commit 4cbf92f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class SolutionLIS {
4+
public int lengthOfLIS(int[] nums) {
5+
int n = nums.length;
6+
int[] dp = new int[n];
7+
Arrays.fill(dp, 1);
8+
9+
for (int current = 1; current < n; current++) {
10+
for (int prev = 0; prev < current; prev++) {
11+
if (nums[prev] < nums[current]) {
12+
dp[current] = Math.max(dp[current], dp[prev] + 1);
13+
}
14+
}
15+
}
16+
17+
int maxLength = 0;
18+
for (int len : dp) {
19+
maxLength = Math.max(maxLength, len);
20+
}
21+
return maxLength;
22+
}
23+
}
24+

0 commit comments

Comments
 (0)