forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
55 lines (51 loc) · 1.35 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
class BinaryIndexedTree {
private int n;
private int[] c;
private int[] d;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
d = new int[n + 1];
}
public void update(int x, int v, int cnt) {
while (x <= n) {
if (c[x] < v) {
c[x] = v;
d[x] = cnt;
} else if (c[x] == v) {
d[x] += cnt;
}
x += x & -x;
}
}
public int[] query(int x) {
int v = 0, cnt = 0;
while (x > 0) {
if (c[x] > v) {
v = c[x];
cnt = d[x];
} else if (c[x] == v) {
cnt += d[x];
}
x -= x & -x;
}
return new int[] {v, cnt};
}
}
public class Solution {
public int findNumberOfLIS(int[] nums) {
// int[] arr = Arrays.stream(nums).distinct().sorted().toArray();
int[] arr = nums.clone();
Arrays.sort(arr);
int m = arr.length;
BinaryIndexedTree tree = new BinaryIndexedTree(m);
for (int x : nums) {
int i = Arrays.binarySearch(arr, x) + 1;
int[] t = tree.query(i - 1);
int v = t[0];
int cnt = t[1];
tree.update(i, v + 1, Math.max(cnt, 1));
}
return tree.query(m)[1];
}
}