Skip to content

Commit 5632cae

Browse files
committed
address comment
1 parent 0099454 commit 5632cae

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

3Sum and 4Sum/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ There are slight optimizations you can do to find `m`, but to keep things simple
111111

112112
#### Avoiding Duplicates
113113

114-
// TODO: Work in progress
115-
116114
Avoiding duplicate values is fairly straightforward if you've understood everything so far. Let's consider a sample array that has a few duplicates:
117115

118116
```
@@ -123,25 +121,44 @@ target = 0
123121

124122
One possible subset is `[-1, 0, 1]`, and in fact is the only subset for 3Sum.
125123

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.
128125

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`.
130127

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
133129

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
135133
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
137137
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
140141
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+
```
142151

143152

144153

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.
145161

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.
146163

147164
[5]: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Two-Sum%20Problem

swift-algorithm-club.xcworkspace/contents.xcworkspacedata

Lines changed: 13 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

t.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)