-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_18.py
129 lines (115 loc) · 4.35 KB
/
prob_18.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 18. 4Sum
# https://leetcode.com/problems/4sum/
# solution 1: 49%
class Solution_0(object):
def fourSum(self, nums, target):
nums.sort()
results = []
for i in range(len(nums)-3):
if i!=0 and nums[i] == nums[i-1]:
continue
target_for_3sum = target - nums[i]
res_3sum = self.threeSum_withtarget(nums[i+1:], target_for_3sum)
for res in res_3sum:
results.append([nums[i]] + res)
return results
def threeSum_withtarget(self, nums, target):
res = []
N = len(nums)
if N < 3: return res
# nums.sort() # already sorted
for i in range(N-2): # j k for last
new_target = target - nums[i]
l, r = i+1, N-1
if i!=0 and nums[i] == nums[i-1]:
continue
while l<r:
cur_sum = nums[l] + nums[r]
if cur_sum > new_target:
r -= 1 #move r
elif cur_sum < new_target:
l += 1# move l
else: # equal to target
res.append([nums[i], nums[l], nums[r]])
while l<r and nums[l] == nums[l+1]:
l += 1
while l<r and nums[r] == nums[r-1]:
r -= 1
r -= 1
l += 1
return res
# solution 2: 100% pass the results into function,
# inmportant: stop when target is not possible
class Solution(object):
def fourSum(self, nums, target):
nums.sort()
results = []
for i in range(len(nums)-3):
if i!=0 and nums[i] == nums[i-1]:
continue
if target < nums[i]*4 or target > nums[-1]*4: # IMP: take advantages of sorted list
break
target_for_3sum = target - nums[i]
self.threeSum_withtarget(nums[i+1:], target_for_3sum, nums[i], results)
# for res in res_3sum:
# results.append([nums[i]] + res)
return results
def threeSum_withtarget(self, nums, target, comb, res):
# res = []
N = len(nums)
if N < 3: return
# nums.sort() # already sorted
for i in range(N-2): # j k for last
new_target = target - nums[i]
l, r = i+1, N-1
if i!=0 and nums[i] == nums[i-1]:
continue
if target < nums[i]*3 or target > nums[-1]*3: # IMP: take advantages of sorted list
break
while l<r:
cur_sum = nums[l] + nums[r]
if cur_sum > new_target:
r -= 1 #move r
elif cur_sum < new_target:
l += 1# move l
else: # equal to target
res.append([comb, nums[i], nums[l], nums[r]])
while l<r and nums[l] == nums[l+1]:
l += 1
while l<r and nums[r] == nums[r-1]:
r -= 1
r -= 1
l += 1
return
# solution 3: very clean code from web
class Solution_2(object):
def fourSum(self, nums, target):
nums.sort()
results = []
self.findNsum(nums, target, 4, [], results)
return results
def findNsum(self, nums, target, N, result, results):
if len(nums) < N or N < 2: return
# solve 2-sum
if N == 2:
l,r = 0,len(nums)-1
while l < r:
if nums[l] + nums[r] == target:
results.append(result + [nums[l], nums[r]])
l += 1
r -= 1
while l < r and nums[l] == nums[l - 1]:
l += 1
while r > l and nums[r] == nums[r + 1]:
r -= 1
elif nums[l] + nums[r] < target:
l += 1
else:
r -= 1
else:
for i in range(0, len(nums)-N+1): # careful about range
if target < nums[i]*N or target > nums[-1]*N: # take advantages of sorted list
break
if i == 0 or i > 0 and nums[i-1] != nums[i]: # recursively reduce N
self.findNsum(nums[i+1:], target-nums[i], N-1, result+[nums[i]], results)
return