forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution3.java
76 lines (59 loc) · 1.89 KB
/
Solution3.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
65
66
67
68
69
70
71
72
73
74
75
76
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description/
/// Author : liuyubobobo
/// Time : 2017-11-19
import java.util.Arrays;
/// Dynamic Programming
/// Best Solution for LIS Problem
///
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
public class Solution3 {
public int lengthOfLIS(int[] nums) {
if(nums.length == 0)
return 0;
// dp[i] is the last num for length i increasing sequence
int dp[] = new int[nums.length + 1];
Arrays.fill(dp, Integer.MIN_VALUE);
int len = 1;
dp[1] = nums[0];
for(int i = 1 ; i < nums.length ; i ++)
if(nums[i] > dp[len]){
len ++;
dp[len] = nums[i];
}
else{
int index = lowerBound(dp, 0, len, nums[i]);
if(dp[index] != nums[i])
dp[index] = Math.min(dp[index], nums[i]);
}
return len;
}
private int lowerBound(int[] arr, int l, int r, int target){
int left = l, right = r + 1;
while(left != right){
int mid = left + (right - left) / 2;
if(arr[mid] >= target)
right = mid;
else // arr[mid] < target
left = mid + 1;
}
return left;
}
public static void main(String[] args) {
int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};
System.out.println((new Solution3()).lengthOfLIS(nums1));
// 4
// ---
int nums2[] = {4, 10, 4, 3, 8, 9};
System.out.println((new Solution3()).lengthOfLIS(nums2));
// 3
// ---
int nums3[] = {2, 2};
System.out.println((new Solution3()).lengthOfLIS(nums3));
// 1
// ---
int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};
System.out.println((new Solution3()).lengthOfLIS(nums4));
// 6
}
}