forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
55 lines (51 loc) · 1.43 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
54
55
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int val) {
while (x <= n) {
this.c[x] = Math.max(this.c[x], val);
x += x & -x;
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s = Math.max(s, this.c[x]);
x -= x & -x;
}
return s;
}
}
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int n = height.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {height[i], weight[i]};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
Set<Integer> s = new HashSet<>();
for (int[] e : arr) {
s.add(e[1]);
}
List<Integer> alls = new ArrayList<>(s);
Collections.sort(alls);
Map<Integer, Integer> m = new HashMap<>(alls.size());
for (int i = 0; i < alls.size(); ++i) {
m.put(alls.get(i), i + 1);
}
BinaryIndexedTree tree = new BinaryIndexedTree(alls.size());
int ans = 1;
for (int[] e : arr) {
int x = m.get(e[1]);
int t = tree.query(x - 1) + 1;
ans = Math.max(ans, t);
tree.update(x, t);
}
return ans;
}
}