Skip to content
LC-2475 Easy LeetCode

2475. Number of Unequal Triplets in Array

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 72% Topics: Array, Hash Table, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n * k) = O(3 * n)
# Space: O(n + k) = O(n)

import collections


# freq table, dp
class Solution(object):
    def unequalTriplets(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        K = 3
        cnt = collections.Counter()
        dp = [0]*K  # dp[i]: number of unequal (i+1)-plets
        for x in nums:
            cnt[x] += 1
            other_cnt = 1
            for i in xrange(K):
                dp[i] += other_cnt
                other_cnt = dp[i]-cnt[x]*other_cnt
        return dp[K-1]

Solution from kamyu104/LeetCode-Solutions · MIT