File tree 3 files changed +129
-0
lines changed
lcci/17.18.Shortest Supersequence
3 files changed +129
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,50 @@ function shortestSeq(big: number[], small: number[]): number[] {
187
187
}
188
188
```
189
189
190
+ ``` swift
191
+ class Solution {
192
+ func shortestSeq (_ big : [Int ], _ small : [Int ]) -> [Int ] {
193
+ let needCount = small.count
194
+ var need = [Int : Int ]()
195
+ var window = [Int : Int ]()
196
+ small.forEach { need[$0 , default : 0 ] += 1 }
197
+
198
+ var count = needCount
199
+ var minLength = Int .max
200
+ var result = (-1 , -1 )
201
+
202
+ var left = 0
203
+ for right in 0 ..< big.count {
204
+ let element = big[right]
205
+ if need[element] != nil {
206
+ window[element, default : 0 ] += 1
207
+ if window[element]! <= need[element]! {
208
+ count -= 1
209
+ }
210
+ }
211
+
212
+ while count == 0 {
213
+ if right - left + 1 < minLength {
214
+ minLength = right - left + 1
215
+ result = (left, right)
216
+ }
217
+
218
+ let leftElement = big[left]
219
+ if need[leftElement] != nil {
220
+ window[leftElement]! -= 1
221
+ if window[leftElement]! < need[leftElement]! {
222
+ count += 1
223
+ }
224
+ }
225
+ left += 1
226
+ }
227
+ }
228
+
229
+ return result.0 == -1 ? [] : [result.0, result.1]
230
+ }
231
+ }
232
+ ```
233
+
190
234
<!-- tabs: end -->
191
235
192
236
<!-- end -->
Original file line number Diff line number Diff line change @@ -191,6 +191,50 @@ function shortestSeq(big: number[], small: number[]): number[] {
191
191
}
192
192
```
193
193
194
+ ``` swift
195
+ class Solution {
196
+ func shortestSeq (_ big : [Int ], _ small : [Int ]) -> [Int ] {
197
+ let needCount = small.count
198
+ var need = [Int : Int ]()
199
+ var window = [Int : Int ]()
200
+ small.forEach { need[$0 , default : 0 ] += 1 }
201
+
202
+ var count = needCount
203
+ var minLength = Int .max
204
+ var result = (-1 , -1 )
205
+
206
+ var left = 0
207
+ for right in 0 ..< big.count {
208
+ let element = big[right]
209
+ if need[element] != nil {
210
+ window[element, default : 0 ] += 1
211
+ if window[element]! <= need[element]! {
212
+ count -= 1
213
+ }
214
+ }
215
+
216
+ while count == 0 {
217
+ if right - left + 1 < minLength {
218
+ minLength = right - left + 1
219
+ result = (left, right)
220
+ }
221
+
222
+ let leftElement = big[left]
223
+ if need[leftElement] != nil {
224
+ window[leftElement]! -= 1
225
+ if window[leftElement]! < need[leftElement]! {
226
+ count += 1
227
+ }
228
+ }
229
+ left += 1
230
+ }
231
+ }
232
+
233
+ return result.0 == -1 ? [] : [result.0, result.1]
234
+ }
235
+ }
236
+ ```
237
+
194
238
<!-- tabs: end -->
195
239
196
240
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func shortestSeq( _ big: [ Int ] , _ small: [ Int ] ) -> [ Int ] {
3
+ let needCount = small. count
4
+ var need = [ Int: Int] ( )
5
+ var window = [ Int: Int] ( )
6
+ small. forEach { need [ $0, default: 0 ] += 1 }
7
+
8
+ var count = needCount
9
+ var minLength = Int . max
10
+ var result = ( - 1 , - 1 )
11
+
12
+ var left = 0
13
+ for right in 0 ..< big. count {
14
+ let element = big [ right]
15
+ if need [ element] != nil {
16
+ window [ element, default: 0 ] += 1
17
+ if window [ element] ! <= need [ element] ! {
18
+ count -= 1
19
+ }
20
+ }
21
+
22
+ while count == 0 {
23
+ if right - left + 1 < minLength {
24
+ minLength = right - left + 1
25
+ result = ( left, right)
26
+ }
27
+
28
+ let leftElement = big [ left]
29
+ if need [ leftElement] != nil {
30
+ window [ leftElement] ! -= 1
31
+ if window [ leftElement] ! < need [ leftElement] ! {
32
+ count += 1
33
+ }
34
+ }
35
+ left += 1
36
+ }
37
+ }
38
+
39
+ return result. 0 == - 1 ? [ ] : [ result. 0 , result. 1 ]
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments