LeetCode 1534 Count Good Triplets (Python)

Posted by 小明MaxMing on August 2, 2020

题目

Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.

A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:

  • 0 <= i < j < k < arr.length
  • arr[i] - arr[j] <= a
  • arr[j] - arr[k] <= b
  • arr[i] - arr[k] <= c  
    Where x denotes the absolute value of x.

Return the number of good triplets.

解题思路

遍历ijk,判断是否符合条件

代码

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        res = 0
        n = len(arr)
        for i in range(n - 2):
            for j in range(i + 1, n - 1):
                if math.fabs(arr[j] - arr[i]) > a:
                    continue
                for k in range(j + 1, n):
                    if math.fabs(arr[k]-arr[i]) <= c or math.fabs(arr[k] - arr[j]) <= b:
                        res += 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅