forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2.java
56 lines (41 loc) · 1.42 KB
/
Solution2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description/
/// Author : liuyubobobo
/// Time : 2017-11-19
import java.util.Arrays;
/// Dynamic Programming
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)
public class Solution2 {
public int lengthOfLIS(int[] nums) {
if(nums.length == 0)
return 0;
// memo[i] is the length of the longest increasing sequence end in nums[i]
int memo[] = new int[nums.length];
Arrays.fill(memo, 1);
for(int i = 1 ; i < nums.length ; i ++)
for(int j = 0 ; j < i ; j ++)
if(nums[i] > nums[j])
memo[i] = Math.max(memo[i], 1 + memo[j]);
int res = memo[0];
for(int i = 1 ; i < nums.length ; i ++)
res = Math.max(res, memo[i]);
return res;
}
public static void main(String[] args) {
int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};
System.out.println((new Solution2()).lengthOfLIS(nums1));
// 4
// ---
int nums2[] = {4, 10, 4, 3, 8, 9};
System.out.println((new Solution2()).lengthOfLIS(nums2));
// 3
// ---
int nums3[] = {2, 2};
System.out.println((new Solution2()).lengthOfLIS(nums3));
// 1
// ---
int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};
System.out.println((new Solution2()).lengthOfLIS(nums4));
// 6
}
}