Skip to content

Commit a7fc635

Browse files
committed
259_3Sum_Smaller
1 parent f27c6cd commit a7fc635

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@
7575
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/228_Summary_Ranges.py) | Detect start and jump, O(n) and O(1) |
7676
| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/243_Shortest_Word_Distance.py) | Update index1 and index2, and check distance, O(n) and O(1) |
7777
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/252_Meeting_Rooms.py) | 1. Sort and compare intervals[i].end with intervals[i+1], O(nlogn) and O(1)<br>2. Sort and check intervals when count >= 2, O(nlogn) and O(n) |
78+
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/259_3Sum_Smaller.py) | 1. Reduce to two sum smaller, then binary search, O(n^2lgn) and O(1)<br>2. Reduce to two sum smaller, then two points, O(n^2) and O(1)|
7879
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
7980
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

python/259_3Sum_Smaller.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution(object):
2+
# def threeSumSmaller(self, nums, target):
3+
# """
4+
# :type nums: List[int]
5+
# :type target: int
6+
# :rtype: int
7+
# """
8+
# # https://leetcode.com/articles/3sum-smaller/#approach-2-binary-search-accepted
9+
# nums.sort()
10+
# ls = len(nums)
11+
# res = 0
12+
# for i in range(ls - 1):
13+
# res += self.twoSumSmaller(nums, i + 1, target - nums[i])
14+
# return res
15+
#
16+
# def twoSumSmaller(self, nums, start, target):
17+
# res = 0
18+
# for i in range(start, len(nums)):
19+
# pos = self.binarySearch(nums, i, target - nums[i])
20+
# res += pos - i
21+
# return res
22+
#
23+
# def binarySearch(self, nums, start, target):
24+
# left, right = start, len(nums) - 1
25+
# while left < right:
26+
# mid = (left + right + 1) / 2
27+
# if nums[mid] < target:
28+
# # left should always less than target
29+
# left = mid
30+
# else:
31+
# right = mid - 1
32+
# return left
33+
34+
def threeSumSmaller(self, nums, target):
35+
"""
36+
:type nums: List[int]
37+
:type target: int
38+
:rtype: int
39+
"""
40+
# https://leetcode.com/articles/3sum-smaller/#approach-2-binary-search-accepted
41+
nums.sort()
42+
ls = len(nums)
43+
res = 0
44+
for i in range(ls - 1):
45+
res += self.twoSumSmaller(nums, i + 1, target - nums[i])
46+
return res
47+
48+
def twoSumSmaller(self, nums, start, target):
49+
res, left, right = 0, start, len(nums) - 1
50+
while left < right:
51+
if nums[left] + nums[right] < target:
52+
res += right - left
53+
left += 1
54+
else:
55+
right -= 1
56+
return res

0 commit comments

Comments
 (0)