3Sum Smaller

Problem Summary

Key Observations

Why This Works

Main Concepts Used

Time & Space Complexity

Code

class Solution:
    def searchTriplets(self, arr, target):
        arr.sort()
        result = 0
        for i, firstNum in enumerate(arr):
            l, r = i + 1, len(arr) - 1
            while l < r:
                threeSum = firstNum + arr[l] + arr[r]
                if threeSum < target:
                    result += r - l
                    l += 1
                else:
                    r -= 1
        return result