Seawolf 发表于 2020-8-2 12:33:28

Leetcode 1534. Count Good Triplets

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, arr, arr) is good if the following conditions are true:

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

Return the number of good triplets.



Example 1:

Input: arr = , a = 7, b = 2, c = 3
Output: 4
Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
Example 2:

Input: arr = , a = 0, b = 0, c = 1
Output: 0
Explanation: No triplet satisfies all conditions.


Constraints:

3 <= arr.length <= 100
0 <= arr <= 1000
0 <= a, b, c <= 1000

class Solution:
    def countGoodTriplets(self, arr: List, a: int, b: int, c: int) -> int:
      result = []
      for i in range(len(arr)):
            for j in range(i + 1, len(arr)):
                for k in range(j + 1, len(arr)):
                  f = arr
                  s = arr
                  t = arr
                  if abs(f - s) <= a and abs(s - t) <= b and abs(f - t) <= c:
                        result.append((f, s, t))
      return len(result)
页: [1]
查看完整版本: Leetcode 1534. Count Good Triplets