The Brute Force Solution Time Complexity: O(N^3) Space Complexity: O(1)
| 1 | class Solution: |
| 2 | """ |
| 3 | The Brute Force Solution |
| 4 | |
| 5 | Time Complexity: O(N^3) |
| 6 | Space Complexity: O(1) |
| 7 | """ |
| 8 | |
| 9 | def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: |
| 10 | |
| 11 | triplet_count = 0 |
| 12 | |
| 13 | # for each i, for each j, check if the first condition is satisfied |
| 14 | for i in range(len(arr) - 2): |
| 15 | for j in range(i + 1, len(arr) - 1): |
| 16 | if abs(arr[i] - arr[j]) <= a: |
| 17 | |
| 18 | # for each k, check if the last two conditions are satisfied |
| 19 | for k in range(j + 1, len(arr)): |
| 20 | if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c: |
| 21 | |
| 22 | # the triplet is Good, increment the count! |
| 23 | triplet_count += 1 |
| 24 | return triplet_count |
nothing calls this directly
no outgoing calls
no test coverage detected