259. 3Sum Smaller

class Solution:
    def threeSumSmaller(self, nums: List[int], target: int) -> int:
        
        nums.sort()
        n = len(nums)

        if n < 3:
            return 0

        result = 0

        for i in range(n - 2):
            l, r = i + 1, n - 1

            while l < r:
                total = nums[i] + nums[l] + nums[r]

                if total < target:
                    result += r - l
                    l += 1
                else:
                    r -= 1

        return result

CleanShot 2024-09-02 at 14.38.25@2x.png