-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq16.10.cpp
152 lines (124 loc) · 4.18 KB
/
q16.10.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
bool isKeyExists(int key, const map<int, int> &hashmap) {
if (hashmap.find(key) == hashmap.end())
return false;
else
return true;
}
void getMaxPopulation_v1(vector<int> &birthYears, vector<int> &deathYears,
int startYear, int endYear) {
map<int, int> yearlyPopCount;
for (int it = startYear; it <= endYear; it++)
yearlyPopCount[it] = 0;
sort(birthYears.begin(), birthYears.end());
sort(deathYears.begin(), deathYears.end());
// since death in an year should reduce dec pop in next year
// also this solves the problem of person born and dead in same year
for (auto &item : deathYears) {
item++;
}
vector<int>::iterator birth_iter, death_iter;
birth_iter = birthYears.begin();
death_iter = deathYears.begin();
int counter = 0;
while ((birth_iter != birthYears.end()) || (death_iter != deathYears.end())) {
if ((*birth_iter <= *death_iter) && (birth_iter != birthYears.end())) {
counter++;
while (*birth_iter == *(birth_iter + 1)) {
counter++;
birth_iter++;
}
assert((isKeyExists(*birth_iter, yearlyPopCount),
"given input is not within the possible year range"));
yearlyPopCount[*birth_iter] += counter;
birth_iter++;
counter = 0;
}
else {
counter++;
while (*death_iter == *(death_iter + 1)) {
counter++;
death_iter++;
}
assert((isKeyExists(*death_iter, yearlyPopCount),
"given input is not within the possible year range"));
yearlyPopCount[*death_iter] -= counter;
death_iter++;
counter = 0;
}
}
// go through all the keys of hashmap
int cum_pop = 0;
int max_pop = 0;
int max_pop_yr = startYear - 1;
for (auto &map_pair : yearlyPopCount) {
cum_pop += map_pair.second;
map_pair.second = cum_pop;
if (cum_pop > max_pop) {
max_pop = cum_pop;
max_pop_yr = map_pair.first;
}
}
cout << "max population is " << max_pop << " in year " << max_pop_yr << endl;
// cout << "cum_pops: " << endl;
// for(auto& map_pair : yearlyPopCount){
// // cout << map_pair.first << ": " << map_pair.second << endl;
// cout << map_pair.second << ", " ;
// }
// cout << endl;
}
void getMaxPopulation_v2(vector<int> &birthYears, vector<int> &deathYears,
int startYear, int endYear) {
map<int, int> yearlyPopCount;
for (int it = startYear; it < endYear; it++)
yearlyPopCount[it] = 0;
for (auto &byear : birthYears) {
assert((isKeyExists(byear, yearlyPopCount),
"given input year is not in expected range"));
yearlyPopCount[byear]++;
}
for (auto &dyear : deathYears) {
assert((isKeyExists(dyear, yearlyPopCount),
"given input year is not in expected range"));
yearlyPopCount[dyear + 1]--;
}
// go through all the keys of hashmap
int cum_pop = 0;
int max_pop = 0;
int max_pop_yr = startYear - 1;
for (auto &map_pair : yearlyPopCount) {
cum_pop += map_pair.second;
map_pair.second = cum_pop;
if (cum_pop > max_pop) {
max_pop = cum_pop;
max_pop_yr = map_pair.first;
}
}
cout << "max population is " << max_pop << " in year " << max_pop_yr << endl;
// for(auto& map_pair : yearlyPopCount){
// // cout << map_pair.first << ": " << map_pair.second << endl;
// cout << map_pair.second << ", " ;
// }
// cout << endl;
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
vector<pair<int, int>> peopleList;
vector<int> birthYears{10, 15, 25, 46, 83, 91, 39, 72, 96, 64};
vector<int> deathYears{40, 29, 25, 48, 100, 98, 64, 100, 101, 92};
cout << "birth years: " << endl;
for (auto &elem : birthYears)
cout << elem << ", ";
cout << endl;
cout << "birth years: " << endl;
for (auto &elem : deathYears)
cout << elem << ", ";
cout << endl;
// getMaxPopulation_v1(birthYears, deathYears, 0, 101);
getMaxPopulation_v2(birthYears, deathYears, 0, 101);
return 0;
}