-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
49 lines (45 loc) · 1.01 KB
/
Solution.cpp
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
class BinaryIndexedTree {
private:
int n;
vector<int> c;
public:
BinaryIndexedTree(int n)
: n(n)
, c(n + 1) {}
void update(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] = max(c[x], v);
}
}
int query(int x) {
int ans = 0;
for (; x > 0; x -= x & -x) {
ans = max(ans, c[x]);
}
return ans;
}
};
class Solution {
public:
int minOperations(vector<int>& target, vector<int>& arr) {
int m = target.size();
unordered_map<int, int> d;
for (int i = 0; i < m; ++i) {
d[target[i]] = i + 1;
}
vector<int> nums;
for (int x : arr) {
if (d.contains(x)) {
nums.push_back(d[x]);
}
}
BinaryIndexedTree tree(m);
int ans = 0;
for (int x : nums) {
int v = tree.query(x - 1) + 1;
ans = max(ans, v);
tree.update(x, v);
}
return m - ans;
}
};