Skip to content

Commit

Permalink
Create 3sum-smaller.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Aug 17, 2015
1 parent 8ac2961 commit 34f0e87
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Python/3sum-smaller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Time: O(n^2)
# Space: O(1)

class Solution:
# @param {integer[]} nums
# @param {integer} target
# @return {integer}
def threeSumSmaller(self, nums, target):
nums.sort()
n = len(nums)

count, k = 0, 2
while k < n:
i, j = 0, k - 1
while i < j: # Two Pointers, linear time.
if nums[i] + nums[j] + nums[k] >= target:
j -= 1
else:
count += j - i
i += 1
k += 1

return count

0 comments on commit 34f0e87

Please sign in to comment.