-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution4.rs
38 lines (34 loc) · 884 Bytes
/
Solution4.rs
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
impl Solution {
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
let mut ans = 0;
let mut nums = nums;
nums.sort();
let n = nums.len();
for i in 1..n - 1 {
let mut l = 0;
let mut r = i;
while l < r {
let mid = (l + r) >> 1;
if nums[mid] >= nums[i] {
r = mid;
} else {
l = mid + 1;
}
}
let j = r;
let mut l = i + 1;
let mut r = n;
while l < r {
let mid = (l + r) >> 1;
if nums[mid] > nums[i] {
r = mid;
} else {
l = mid + 1;
}
}
let k = r;
ans += j * (n - k);
}
ans as i32
}
}