Skip to content
LC-1534 Easy LeetCode

1534. Count Good Triplets

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 85% Topics: Array, Enumeration
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^3)
# Space: O(1)

class Solution(object):
    def countGoodTriplets(self, arr, a, b, c):
        """
        :type arr: List[int]
        :type a: int
        :type b: int
        :type c: int
        :rtype: int
        """
        return sum(abs(arr[i]-arr[j]) <= a and
                   abs(arr[j]-arr[k]) <= b and
                   abs(arr[k]-arr[i]) <= c 
                   for i in xrange(len(arr)-2)
                       for j in xrange(i+1, len(arr)-1)
                           for k in xrange(j+1, len(arr)))
  

Solution from kamyu104/LeetCode-Solutions · MIT