forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-array-empty.cpp
80 lines (73 loc) · 2.03 KB
/
make-array-empty.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Time: O(nlogn)
// Space: O(n)
// sort
class Solution {
public:
long long countOperationsToEmptyArray(vector<int>& nums) {
vector<int> idxs(size(nums));
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return nums[a] < nums[b];
});
int64_t result = size(idxs);
for (int i = 0; i + 1 < size(idxs); ++i) {
if (idxs[i] > idxs[i + 1]) {
result += size(idxs) - (i + 1);
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// sort, bit, fenwick tree
class Solution2 {
public:
long long countOperationsToEmptyArray(vector<int>& nums) {
vector<int> idxs(size(nums));
iota(begin(idxs), end(idxs), 0);
sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {
return nums[a] < nums[b];
});
BIT bit(size(nums));
int64_t result = size(nums);
int prev = -1;
for (const auto& i : idxs) {
if (prev == -1) {
result += i;
} else if (prev < i) {
result += (i - prev) - (bit.query(i) - bit.query(prev - 1));
} else {
result += ((size(nums) - 1) - bit.query(size(nums) - 1)) - ((prev - i) - (bit.query(prev) - bit.query(i - 1)));
}
bit.add(i, 1);
prev = i;
}
return result;
}
private:
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
};