Skip to content

Instantly share code, notes, and snippets.

@DixieKorley
Last active June 20, 2019 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DixieKorley/9ca07423e6b2acc63e4caa76bc8ece6c to your computer and use it in GitHub Desktop.
Save DixieKorley/9ca07423e6b2acc63e4caa76bc8ece6c to your computer and use it in GitHub Desktop.
Three sum smallest problem
"""Three Sum Smallest"""
def three_sum_smaller(nums, target):
n = len(nums)
if nums is None or n < 3:
return 0
result = 0
for left in range(n - 2):
middle = left + 1
right = n - 1
while middle < right:
total = nums[left] + nums[middle] + nums[right]
if total < target:
result += right - middle
middle += 1
else:
right -= 1
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment