You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3Sum and 4Sum/README.md
+29-12Lines changed: 29 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,8 +111,6 @@ There are slight optimizations you can do to find `m`, but to keep things simple
111
111
112
112
#### Avoiding Duplicates
113
113
114
-
// TODO: Work in progress
115
-
116
114
Avoiding duplicate values is fairly straightforward if you've understood everything so far. Let's consider a sample array that has a few duplicates:
117
115
118
116
```
@@ -123,25 +121,44 @@ target = 0
123
121
124
122
One possible subset is `[-1, 0, 1]`, and in fact is the only subset for 3Sum.
125
123
126
-
## 4Sum
127
-
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
124
+
The easiet way is using set. We can maintain a solution set, then we can check if the triplets is in the set or not to determine whether it's duplicate.
128
125
129
-
**Note**: The solution set must not contain duplicate quadruplets.
126
+
Set introduces space complexity. So we still want to avoid extra space using. Let's change an angle consider, we can loop the array first find `m` then next thing is to find `l` and `r`.
130
127
131
-
### Solution
132
-
After 3Sum, you should have feeling actually we just need a same idea to downgrade it to 3Sum, and then 2Sum, and then solve it.
128
+
For example
133
129
134
-
How? I will leave it as a challenge for you to figure out first and see if you really master the idea behind this kind of problems.
130
+
```
131
+
```
132
+
[-1, 0, 1, 2, -1, -4] // unsorted
135
133
136
-
Feel free to check out the solution if you are blocked.
134
+
1)
135
+
[-4, -1, -1, 0, 1, 2]
136
+
m l r
137
137
138
-
## Where to go next?
139
-
If it’s a KSum, and `K` is a big number, do we need to create `K` pointers and solve it?
138
+
2)
139
+
[-4, -1, -1, 0, 1, 2]
140
+
m l r
140
141
141
-
I will write another topic to present how we will solve this KSum problem with a generic way soon.
142
+
3)
143
+
[-4, -1, -1, 0, 1, 2]
144
+
m l r
145
+
```
146
+
147
+
We loop `m` in `0..<n`. We will do another inner loop at the same time, `l..r` loops in `i+1..<n`.
148
+
In 1), we will check if `a[i] == a[i-1]`? It's not in this case, then the problem is 2sum (`l..r`).
149
+
In 2), Since `a[i] == a[i-1]`, it means `a[i-1]` covers `a[i]` case. Because case 3) contains case 2) solutions.
150
+
```
142
151
143
152
144
153
154
+
## 4Sum
155
+
Given an array S of n integers, find all subsets of the array with 4 values where the 4 values sum up to a target number.
156
+
157
+
**Note**: The solution set must not contain duplicate quadruplets.
158
+
159
+
### Solution
160
+
After 3Sum, we already have the idea to change to a problem to a familiar problem we solved before. So, the idea here is straightforward. We just need to downgrade 4Sum to 3Sum. Then we can solve 4Sum.
145
161
162
+
It's easy to think that we loop the array and get the first the element, then the rest array is 3Sum problem. Since the code is pretty simple, I will avoid duplicate introducation here.
0 commit comments