Skip to content

LC 0259 [M] 3Sum Smaller

Code with Senpai edited this page Sep 23, 2020 · 4 revisions
class Solution:
    def threeSumSmaller(self, arr: List[int], target: int) -> int:
        arr.sort()
        count = 0
        n = len(arr)
        for i in range(n - 2)):
            l, r = i + 1, n - 1
            while l < r:
                s = arr[i] + arr[l] + arr[r]
                if s < target:
                    count += r - l
                    l += 1
                else:
                    r -= 1
        return count
Clone this wiki locally