File tree Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -173,6 +173,35 @@ impl Solution {
173
173
}
174
174
```
175
175
176
+ ``` swift
177
+ class Solution {
178
+ func maxAliveYear (_ birth : [Int ], _ death : [Int ]) -> Int {
179
+ let base = 1900
180
+ var delta = Array (repeating : 0 , count : 102 ) // Array to hold the changes
181
+
182
+ for i in 0 ..< birth.count {
183
+ let start = birth[i] - base
184
+ let end = death[i] - base
185
+ delta[start] += 1
186
+ if end + 1 < delta.count {
187
+ delta[end + 1 ] -= 1
188
+ }
189
+ }
190
+
191
+ var maxAlive = 0 , currentAlive = 0 , maxYear = 0
192
+ for year in 0 ..< delta.count {
193
+ currentAlive += delta[year]
194
+ if currentAlive > maxAlive {
195
+ maxAlive = currentAlive
196
+ maxYear = year + base
197
+ }
198
+ }
199
+
200
+ return maxYear
201
+ }
202
+ }
203
+ ```
204
+
176
205
<!-- tabs: end -->
177
206
178
207
<!-- end -->
Original file line number Diff line number Diff line change @@ -181,6 +181,35 @@ impl Solution {
181
181
}
182
182
```
183
183
184
+ ``` swift
185
+ class Solution {
186
+ func maxAliveYear (_ birth : [Int ], _ death : [Int ]) -> Int {
187
+ let base = 1900
188
+ var delta = Array (repeating : 0 , count : 102 ) // Array to hold the changes
189
+
190
+ for i in 0 ..< birth.count {
191
+ let start = birth[i] - base
192
+ let end = death[i] - base
193
+ delta[start] += 1
194
+ if end + 1 < delta.count {
195
+ delta[end + 1 ] -= 1
196
+ }
197
+ }
198
+
199
+ var maxAlive = 0 , currentAlive = 0 , maxYear = 0
200
+ for year in 0 ..< delta.count {
201
+ currentAlive += delta[year]
202
+ if currentAlive > maxAlive {
203
+ maxAlive = currentAlive
204
+ maxYear = year + base
205
+ }
206
+ }
207
+
208
+ return maxYear
209
+ }
210
+ }
211
+ ```
212
+
184
213
<!-- tabs: end -->
185
214
186
215
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func maxAliveYear( _ birth: [ Int ] , _ death: [ Int ] ) -> Int {
3
+ let base = 1900
4
+ var delta = Array ( repeating: 0 , count: 102 ) // Array to hold the changes
5
+
6
+ for i in 0 ..< birth. count {
7
+ let start = birth [ i] - base
8
+ let end = death [ i] - base
9
+ delta [ start] += 1
10
+ if end + 1 < delta. count {
11
+ delta [ end + 1 ] -= 1
12
+ }
13
+ }
14
+
15
+ var maxAlive = 0 , currentAlive = 0 , maxYear = 0
16
+ for year in 0 ..< delta. count {
17
+ currentAlive += delta [ year]
18
+ if currentAlive > maxAlive {
19
+ maxAlive = currentAlive
20
+ maxYear = year + base
21
+ }
22
+ }
23
+
24
+ return maxYear
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments