[Leetcode] 1534. Count Good Triplets

whitehousechef·2025년 4월 14일

initial

this is just very basic triple for loop

sol

but need to care the "continue" step cuz we dont wanna early break.

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        length = len(arr)
        ans=0
        for i in range(length-2):
            for j in range(i+1,length-1):
                if abs(arr[i]-arr[j])>a:
                    continue
                for k in range(j+1,length):
                    if abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
                        ans+=1
        return ans

complexity

n^3 time
1 space

0개의 댓글