Skip to content

Commit 38b32d2

Browse files
authored
Updated for FourSum.
1 parent 97911ef commit 38b32d2

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

3Sum and 4Sum/README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) ->
7878
var ret: [[T.Element]] = []
7979
var l = sorted.startIndex
8080
81-
while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
81+
ThreeSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
8282
var m = sorted.index(after: l)
8383
var r = sorted.index(before: sorted.endIndex)
8484
85-
while m < r && r < sorted.endIndex {
85+
TwoSum: while m < r && r < sorted.endIndex {
8686
let sum = sorted[l] + sorted[m] + sorted[r]
8787
if sum == target {
8888
ret.append([sorted[l], sorted[m], sorted[r]])
@@ -101,13 +101,60 @@ func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) ->
101101
```
102102

103103
## 4Sum
104+
104105
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.
105106

106107
**Note**: The solution set must not contain duplicate quadruplets.
107108

108109
### Solution
109-
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.
110110

111-
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.
111+
Foursum is a very straightforward extension to the threeSum algorithm. In threeSum, you kept track of 3 indices:
112+
113+
```
114+
m -> <- r
115+
[-4, -1, -1, 0, 1, 2]
116+
 l
117+
```
118+
119+
For fourSum, you'll keep track of 4:
120+
121+
```
122+
mr -> <- r
123+
[-4, -1, -1, 0, 1, 2]
124+
 l ml ->
125+
```
126+
127+
Here's the code for it (notice it is very similar to 3Sum):
128+
129+
```
130+
func fourSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
131+
let sorted = collection.sorted()
132+
var ret: [[T.Element]] = []
133+
134+
var l = sorted.startIndex
135+
FourSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
136+
var ml = sorted.index(after: l)
137+
138+
ThreeSum: while ml < sorted.endIndex { defer { sorted.formUniqueIndex(after: &ml) }
139+
var mr = sorted.index(after: ml)
140+
var r = sorted.index(before: sorted.endIndex)
141+
142+
TwoSum: while mr < r && r < sorted.endIndex {
143+
let sum = sorted[l] + sorted[ml] + sorted[mr] + sorted[r]
144+
if sum == target {
145+
ret.append([sorted[l], sorted[ml], sorted[mr], sorted[r]])
146+
sorted.formUniqueIndex(after: &mr)
147+
sorted.formUniqueIndex(before: &r)
148+
} else if sum < target {
149+
sorted.formUniqueIndex(after: &mr)
150+
} else {
151+
sorted.formUniqueIndex(before: &r)
152+
}
153+
}
154+
}
155+
}
156+
return ret
157+
}
158+
```
112159

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

0 commit comments

Comments
 (0)