Skip to content

Commit 3ce6600

Browse files
authored
Update maximum-population-year.cpp
1 parent 912bec2 commit 3ce6600

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

C++/maximum-population-year.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ class Solution {
1111
++years[logs[i][0] - MIN_YEAR];
1212
--years[logs[i][1] - MIN_YEAR];
1313
}
14-
int result = 0, max_total = 0, total = 0;
14+
int result = 0;
1515
for (int i = 0; i < size(years); ++i) {
16-
total += years[i];
17-
if (total > max_total) {
18-
max_total = total;
19-
result = i + MIN_YEAR;
16+
if (i) {
17+
years[i] += years[i - 1];
18+
}
19+
if (years[i] > years[result]) {
20+
result = i;
2021
}
2122
}
22-
return result;
23+
return result + MIN_YEAR;
2324
}
2425
};

0 commit comments

Comments
 (0)