Last active
June 20, 2019 22:31
-
-
Save DixieKorley/9ca07423e6b2acc63e4caa76bc8ece6c to your computer and use it in GitHub Desktop.
Three sum smallest problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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