-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path259. 3Sum Smaller.py
More file actions
30 lines (30 loc) · 1.14 KB
/
Copy path259. 3Sum Smaller.py
File metadata and controls
30 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def threeSumSmaller(self, nums: List[int], target: int) -> int:
#soln 1 - Leetcode two pointers, fast, Time O(N^2), Space O(1)
ans, n = 0, len(nums)
nums.sort()
for i in range(n-2):
target2 = target - nums[i]
l, r = i+1, n-1
while l < r:
if nums[l] + nums[r] < target2:
ans += r-l
l += 1
else:
r -= 1
return ans
# #soln 0 - first attempt, two pointers, slow
# ans, n = 0, len(nums)
# nums.sort()
# for i in range(n):
# target2 = target - nums[i]
# for r in range(n-1, i+1, -1):
# l = i+1
# if nums[l] + nums[r] >= target2:
# continue
# while l < r and nums[l] + nums[r] < target2:
# ans += 1
# l += 1
# return ans