Skip to content

Commit 071ccbb

Browse files
authored
Create two-sum-less-than-k.py
1 parent db575cf commit 071ccbb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: Python/two-sum-less-than-k.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def twoSumLessThanK(self, A, K):
6+
"""
7+
:type A: List[int]
8+
:type K: int
9+
:rtype: int
10+
"""
11+
A.sort()
12+
result = -1
13+
left, right = 0, len(A)-1
14+
while left < right:
15+
if A[left]+A[right] >= K:
16+
right -= 1
17+
else:
18+
result = max(result, A[left]+A[right])
19+
left += 1
20+
return result

0 commit comments

Comments
 (0)