-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongestIncreasingSubsequence.java
46 lines (39 loc) · 1.11 KB
/
LongestIncreasingSubsequence.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/longest-increasing-subsequence/
public class LongestIncreasingSubsequence {
private final List<Integer> incr = new ArrayList<>();
private final int[] input;
public LongestIncreasingSubsequence(int[] input) {
this.input = input;
}
public int solution() {
incr.add(input[0]);
for (int i = 1; i < input.length; i++) {
int num = input[i];
if (num > incr.get(incr.size() - 1)) {
incr.add(num);
} else {
incr.set(binarySearch(num), num);
}
}
return incr.size();
}
private int binarySearch(int num) {
int l = 0;
int r = incr.size() - 1;
while (l <= r) {
int m = (l + r) / 2;
if (incr.get(m) == num) {
return m;
}
if (incr.get(m) < num) {
l = m + 1;
} else {
r = m - 1;
}
}
return l;
}
}