MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

Python/count-good-triplets.py:1–24  ·  view source on GitHub ↗

The Brute Force Solution Time Complexity: O(N^3) Space Complexity: O(1)

Source from the content-addressed store, hash-verified

1class 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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected