-
Notifications
You must be signed in to change notification settings - Fork 155
/
problem_16_10_livingPeople.cpp
30 lines (28 loc) · 1.17 KB
/
problem_16_10_livingPeople.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "problem_16_10_livingPeople.h"
namespace chapter_16 {
int livingPeople(const std::vector<Person>& people, int startYear, int endYear) {
if (startYear >= endYear) return startYear;
const int numYears = endYear - startYear + 1; // 101 years from 1900 to 2000
std::vector<int> years(numYears, 0);
for (const Person& p : people) {
if (p.birth >= startYear && p.birth <= endYear) {
years[p.birth - startYear]++;
}
if (p.death >= startYear && p.death < endYear) { // strict less than to conform to rule (see below)
years[p.death + 1 - startYear] --; // rule in problem statement: if someone was alive for any part of a year
// they are considered to have been alive the whole year
}
}
int bestYear = startYear;
int mostPeople = 0;
int currentPeople = 0;
for (int i = 0; i < numYears; i++) {
currentPeople += years[i];
if (currentPeople > mostPeople) {
mostPeople = currentPeople;
bestYear = i + startYear;
}
}
return bestYear;
}
}