1534. Count-Good-Triplets

Question Link

Difficulty: Easy

We have the constraint that the length of the arr is at most 100. This means that we are able to solve this question using a brute force approach where we just iterate through the entire arr 3 times, comparing i, j and k to see if we meet the conditions.

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
        """
        res = 0
        for i in range(len(arr) - 2):
            for j in range(i + 1,  len(arr) - 1):
                for k in range(j + 1, len(arr)):
                    cond1 = abs(arr[i] - arr[j]) <= a
                    cond2 = abs(arr[j] - arr[k]) <= b
                    cond3 = abs(arr[i] - arr[k]) <= c

                    if cond1 and cond2 and cond3:
                        res += 1
        
        return res

Time Complexity: O(n^3)

Space Complexity: O(1)

Time Taken: 4m 46s