forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1.java
64 lines (46 loc) · 1.54 KB
/
Solution1.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
57
58
59
60
61
62
63
64
import java.util.Arrays;
/// 300. Longest Increasing Subsequence
/// https://leetcode.com/problems/longest-increasing-subsequence/description/
/// 记忆化搜索
/// 时间复杂度: O(n^2)
/// 空间复杂度: O(n)
public class Solution1 {
private int[] memo;
public int lengthOfLIS(int[] nums) {
if(nums.length == 0)
return 0;
memo = new int[nums.length];
Arrays.fill(memo, -1);
int res = 1;
for(int i = 0 ; i < nums.length ; i ++)
res = Math.max(res, getMaxLength(nums, i));
return res;
}
// 以 nums[index] 为结尾的最长上升子序列的长度
private int getMaxLength(int[] nums, int index){
if(memo[index] != -1)
return memo[index];
int res = 1;
for(int i = 0 ; i <= index-1 ; i ++)
if(nums[index] > nums[i])
res = Math.max(res, 1 + getMaxLength(nums, i));
return memo[index] = res;
}
public static void main(String[] args) {
int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};
System.out.println((new Solution1()).lengthOfLIS(nums1));
// 4
// ---
int nums2[] = {4, 10, 4, 3, 8, 9};
System.out.println((new Solution1()).lengthOfLIS(nums2));
// 3
// ---
int nums3[] = {2, 2};
System.out.println((new Solution1()).lengthOfLIS(nums3));
// 1
// ---
int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};
System.out.println((new Solution1()).lengthOfLIS(nums4));
// 6
}
}