You are given a 0-indexed array of positive integers nums
. Find the number of triplets (i, j, k)
that meet the following conditions:
0 <= i < j < k < nums.length
nums[i]
,nums[j]
, andnums[k]
are pairwise distinct.- In other words,
nums[i] != nums[j]
,nums[i] != nums[k]
, andnums[j] != nums[k]
.
- In other words,
Return the number of triplets that meet the conditions.
Input: nums = [4,4,2,4,3] Output: 3 Explanation: The following triplets meet the conditions: - (0, 2, 4) because 4 != 2 != 3 - (1, 2, 4) because 4 != 2 != 3 - (2, 3, 4) because 2 != 4 != 3 Since there are 3 triplets, we return 3. Note that (2, 0, 4) is not a valid triplet because 2 > 0.
Input: nums = [1,1,1,1,1] Output: 0 Explanation: No triplets meet the conditions so we return 0.
3 <= nums.length <= 100
1 <= nums[i] <= 1000
use std::collections::HashMap;
impl Solution {
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
let mut count = HashMap::new();
let mut ret = 0;
for num in &nums {
count.entry(num).and_modify(|c| *c += 1).or_insert(1);
}
let count = count.values().collect::<Vec<_>>();
for i in 0..count.len() {
for j in i + 1..count.len() {
for k in j + 1..count.len() {
ret += count[i] * count[j] * count[k];
}
}
}
ret
}
}