Skip to content

Commit e6a1a3f

Browse files
Lintcode/src/chapter4_DynamicProgrammingI/LongestIncreasingSubsequence.java
1 parent 734aca2 commit e6a1a3f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package chapter4_DynamicProgrammingI;
2+
3+
public class LongestIncreasingSubsequence {
4+
5+
/**
6+
* @param nums: The integer array
7+
* @return: The length of LIS (longest increasing subsequence)
8+
*/
9+
public static int longestIncreasingSubsequence(int[] nums) {
10+
// write your code here
11+
if(nums == null || nums.length == 0) return 0;
12+
13+
int[] dp = new int[nums.length];
14+
int max = 0;
15+
for(int i = 0; i < nums.length; i++){
16+
dp[i] = 1;
17+
for(int j = 0; j < i; j++){
18+
if(nums[j] < nums[i]){
19+
dp[i] = (dp[j]+1 > dp[i]) ? dp[j]+1 : dp[i];
20+
}
21+
}
22+
if(dp[i] > max) max = dp[i];
23+
}
24+
25+
return max;
26+
}
27+
28+
public static void main(String...args){
29+
int[] nums = new int[]{9,3,6,2,7};
30+
System.out.println(longestIncreasingSubsequence(nums));
31+
}
32+
}

0 commit comments

Comments
 (0)