LeetCode solutions

1656. Count Good Triplets

My accepted Python solution to LeetCode problem 1656, Count Good Triplets, running in 165ms.

  • Difficulty: Easy
  • Python
  • Runtime 165ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 165ms, memory 17.4MB, accepted 2025-12-29.

python
class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 0
        n = len(arr)
        for i in range(n):
            for j in range(i + 1, n):
                if abs(arr[i] - arr[j]) <= a:
                    for k in range(j + 1, n):
                        if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                            count += 1
        return count

Source