2475. Number of Unequal Triplets in Array
SourceWeekly Contest 320 Q1DifficultyEasyRating1255
Description
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.lengthnums[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.
Example 1:
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.
Example 2:
Input: nums = [1,1,1,1,1] Output: 0 Explanation: No triplets meet the conditions so we return 0.
Constraints:
3 <= nums.length <= 1001 <= nums[i] <= 1000
Solutions
Solution 1: Brute Force Enumeration
Thinking
With \(n\le 100\), ordered triples fit in a triple loop that tests pairwise inequality.
We can directly enumerate all triples \((i, j, k)\) and count all the ones that meet the conditions.
The time complexity is \(O(n^3)\), where \(n\) is the length of the array \(nums\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Solution 2: Sorting + Enumeration of Middle Elements + Binary Search
Thinking
Method 1 is \(O(n^3)\). After sorting, equals sit together. Fix the middle index \(j\); the product of how many values are strictly smaller on the left and strictly larger on the right is the contribution. Two binary searches find the borders.
We can also sort the array \(nums\) first.
Then traverse \(nums\), enumerate the middle element \(nums[j]\), and use binary search to find the nearest index \(i\) on the left side of \(nums[j]\) such that \(nums[i] < nums[j]\); find the nearest index \(k\) on the right side of \(nums[j]\) such that \(nums[k] > nums[j]\). Then the number of triples with \(nums[j]\) as the middle element and meeting the conditions is \((i + 1) \times (n - k)\), which is added to the answer.
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(\log n)\). Here, \(n\) is the length of the array \(nums\).
1 2 3 4 5 6 7 8 9 | |
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 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Solution 3: Hash Table
Thinking
Method 2 still sorts. Only distinct values matter, not indices, so count frequencies: for a middle count \(b\), with \(a\) already seen and \(c=n-a-b\), add \(a\cdot b\cdot c\). Linear.
We can also use a hash table \(cnt\) to count the number of each element in the array \(nums\).
Then traverse the hash table \(cnt\), enumerate the number of middle elements \(b\), and denote the number of elements on the left as \(a\). Then the number of elements on the right is \(c = n - a - b\). At this time, the number of triples that meet the conditions is \(a \times b \times c\), which is added to the answer. Then update \(a = a + b\) and continue to enumerate the number of middle elements \(b\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(nums\).
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |