LeetCode #1534. Count Good Triplets
题目
解题方法
官方推荐:暴力破解...
时间复杂度:O(n^3)
空间复杂度:O(1)
代码
class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
rat = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
if abs(arr[i] - arr[j]) <= a \
and abs(arr[j] - arr[k]) <= b \
and abs(arr[i] - arr[k]) <= c:
rat += 1
return rat