-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3994.cpp
More file actions
30 lines (29 loc) · 798 Bytes
/
Copy path3994.cpp
File metadata and controls
30 lines (29 loc) · 798 Bytes
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
class Solution {
public:
int minAdjacentSwaps(vector<int>& nums, int a, int b) {
int n = nums.size();
vector<int> groups(n, -1);
for (int i = 0; i < n; ++i) {
if (nums[i] < a) groups[i] = 0;
if (nums[i] >= a && nums[i] <= b) groups[i] = 1;
if (nums[i] > b) groups[i] = 2;
}
long long res = 0;
long long one = 0;
long long two = 0;
for (int i = 0; i < n; ++i) {
if (groups[i] == 0) {
res += (one + two);
}
if (groups[i] == 1) {
res += two;
one++;
}
if (groups[i] == 2) {
two++;
}
}
long long mod = 1e9 + 7;
return res % mod;
}
};