-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path018_4Sum.py
More file actions
85 lines (67 loc) · 2.65 KB
/
018_4Sum.py
File metadata and controls
85 lines (67 loc) · 2.65 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
18. 4Sum
Medium
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
"""
# the main idea for 4 sun is as the following:
# 4 sum = i + 3 sum
# j == i + 1 then the problem become 4 sum = i + j + 2 sum
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n = len(nums)
result = []
nums.sort()
# loop for first num, n times at 4 sum level
for i in range(n-3):
# skip uneccesary case
if nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target:
break
# skip uneccesary case
if nums[i] + nums[n-1] + nums[n-2] + nums[n-3] < target:
continue
# skip duplication
if i > 0 and nums[i] == nums[i-1]:
continue
# loop for second number, n times at 3 sum level
for j in range(i + 1, len(nums) - 2):
# skip duplication
if j != i +1 and nums[j] == nums[j-1]:
continue
# skip uneccesary case
if nums[j]+nums[j+1]+nums[j+2] > target - nums[i]:
break
# search for last 2 nums, same as 2Sum problem using 2 pointer lef and right
l, r = j+1, n-1
while l < r:
tmp = nums[i] + nums[j] + nums[l] + nums[r]
if tmp > target:
r -= 1
elif tmp < target:
l +=1
else:
# when tmp == target
result.append([nums[i], nums[j] , nums[l] , nums[r]])
# skip duplication
while r > l and nums[l] == nums[l+1]:
l +=1
while r > l and nums[r] == nums[r-1]:
r -=1
skip duplication and increment
l, r = l + 1, r - 1
return result
"""
Success
Details
Runtime: 88 ms, faster than 95.57% of Python3 online submissions for 4Sum.
Memory Usage: 13.7 MB, less than 7.14% of Python3 online submissions for 4Sum.
"""