-
Notifications
You must be signed in to change notification settings - Fork 5
/
Solution.java
53 lines (44 loc) · 1.34 KB
/
Solution.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
class Solution {
public int lengthOfLIS(int[] nums) {
// Dynamic programming (O(n^2))
int[] lis = new int[nums.length];
Arrays.fill(lis, 1);
int max = 0;
for (int i = nums.length - 1; i >= 0; --i)
for (int j = i + 1; j < nums.length; ++j)
if (nums[i] < nums[j]) lis[i] = Math.max(lis[i], 1 + lis[j]);
if (lis[i] > max) max = lis[i];
return max;
// Dynamic programming (O(nlog(n)))
// don't think I can come up with this idea during the interview, need to practice
// int[] tails = new int[nums.length];
// int size = 0;
// for (int x : nums) {
// int i = 0, j = size;
// while (i != j) {
// int m = (i + j) / 2;
// if (tails[m] < x)
// i = m + 1;
// else
// j = m;
// }
// tails[i] = x;
// if (i == size) ++size;
// }
// return size;
// Brute force, won't work
// int maxLen = 1;
// for (int i = 0; i < nums.length - 1; ++i) {
// int cur = nums[i];
// int curLen = 1;
// for (int j = i + 1; j < nums.length; ++j) {
// if (nums[j] > cur) {
// cur = nums[j];
// curLen++;
// }
// if (curLen > maxLen) maxLen = curLen;
// }
// }
// return maxLen;
}
}