259. 3Sum Smaller
https://leetcode.com/problems/3sum-smaller/
Input: nums = [-2,0,1,3], and target = 2
Output: 2
Explanation: Because there are two triplets which sums are less than 2:
[-2,0,1]
[-2,0,3]Thoughts
Code
class Solution:
def threeSumSmaller(self, nums: List[int], target: int) -> int:
nums.sort()
res = 0
for i, num in enumerate(nums):
j, k = 0, i - 1
while j < k:
if nums[j] + nums[k] < target - num:
res += k - j
j += 1
else:
k -= 1
return resAnalysis
Last updated