-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
47 lines (43 loc) · 1.11 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
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
this.c = new int[n + 1];
}
public void update(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] = Math.max(c[x], v);
}
}
public int query(int x) {
int ans = 0;
for (; x > 0; x -= x & -x) {
ans = Math.max(ans, c[x]);
}
return ans;
}
}
class Solution {
public int minOperations(int[] target, int[] arr) {
int m = target.length;
Map<Integer, Integer> d = new HashMap<>(m);
for (int i = 0; i < m; i++) {
d.put(target[i], i + 1);
}
List<Integer> nums = new ArrayList<>();
for (int x : arr) {
if (d.containsKey(x)) {
nums.add(d.get(x));
}
}
BinaryIndexedTree tree = new BinaryIndexedTree(m);
int ans = 0;
for (int x : nums) {
int v = tree.query(x - 1) + 1;
ans = Math.max(ans, v);
tree.update(x, v);
}
return m - ans;
}
}