Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0dcbe6a

Browse files
feat: add swift implementation to lcci problem: No.16.10 (doocs#2742)
1 parent 7063165 commit 0dcbe6a

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

‎lcci/16.10.Living People/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ impl Solution {
173173
}
174174
```
175175

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+
176205
<!-- tabs:end -->
177206

178207
<!-- end -->

‎lcci/16.10.Living People/README_EN.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,35 @@ impl Solution {
181181
}
182182
```
183183

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+
184213
<!-- tabs:end -->
185214

186215
<!-- end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /