## Monday, July 4, 2016

### Living People

http://stackoverflow.com/questions/31522450/find-the-year-with-the-most-number-of-people-alive-in-python
Given a list of people with their birth and end years (all between `1900` and `2000`), find the year with the most number of people alive.
You may assume that all people were born between 1900 and 2000 (inclusive). If a person was alive during any portion of that year, they should be included in that year's count. For example, Person (birth= 1908, death= 1909) is included in the counts for both 1908 and 1909.

public class Person {
public int birth;
public int death;
public Person(int birthYear, int deathYear) {
birth = birthYear;
death= deathYear;
}
}

The important thing here is to actually use a Person object. This shows better style than, say, having an integer array for birth years and an integer array for death years (with an implicit association of births [i] and deaths [ i] being associated with the same person). You don't get a lot of chances to demonstrate great coding style, so it's valuable to take the ones you get.

There's a simple fix: instead of decrementing array[deathYear], we should decrement array[deathYear + 1].
int maxAliveYear(Person[] people, int min, int max) {
/* Build population delta array. */
int[] populationDeltas = getPopulationDeltas(people, min, max);
int maxAliveYear = getMaxAliveYear(populationDeltas);
return maxAliveYear + min;
}

/* Add birth and death years to deltas array. */
int[] getPopulationDeltas(Person[] people, int min, int max) {
int[] populationDeltas = new int[max - min + 2];
for (Person person : people) {
int birth= person.birth - min;
populationDeltas[birth]++;

int death= person.death - min;
populationDeltas[death + 1]--;
}
return populationDeltas;
}

/* Compute running sums and return index with max. */
int getMaxAliveYear(int[] deltas) {
int maxAliveYear = 0;
int maxAlive = 0;
int currentlyAlive = 0;
for (int year = 0; year < deltas.length; year++) {
currentlyAlive += deltas[year];
if (currentlyAlive > maxAlive) {
maxAliveYear = year;
maxAlive = currentlyAlive;
}
}

return maxAliveYear;
}

This algorithm will take O ( P log P) time, where P is the number of people.
int maxAliveYear(Person[] people, int min, int max) {
int[] births getSortedYears(people, true);
int[] deaths = getSortedYears(people, false);

int birthindex = 0;
int deathindex = 0;
int currentlyAlive 0·,
int maxAlive = 0;
int maxAliveYear = min;
/* Walk through arrays. */
while (birth !ndex < births.length) {
if (births[birthindex] <= deaths[death !ndex]) {
currentlyAlive++; // include birth
if (currentlyAlive > maxAlive) {
maxAlive = currentlyAlive;
maxAliveYear = births[birthindex];
}
birth !ndex++; // move birth index
} else if (births[birth !ndex] > deaths[death !ndex]) {
currentlyAlive--; // include death
deathindex++; // move death index
}
}

return maxAliveYear;
}

/* Copy birth years or death years (depending on the value of copyBirthVear into
* integer array, then sort array. */
int[] getSortedYears(Person[] people, boolean copyBirthYear) {
int[] years = new int[people.length];
for (inti= 0; i < people.length; i++) {
years[i] = copyBirthYear ? people[i].birth : people[i].death;
}
Arrays.sort(years);
return years;
}

A slightly better way of doing this is to create an array where we track the number of people born in each year. Then, we iterate through the list of people and increment the array for each year they are alive.

int maxAliveYear(Person[] people, int min, int max) {
int[] years = createYearMap(people, min, max);
int best = getMaxlndex(years);
return best + min;
}

/* Add each person's years to a year map. */
int[] createYearMap(Person[] people, int min, int max) {
int[] years = new int[max - min + 1];
for (Person person : people) {
incrementRange(years, person.birth - min, person.death - min);
}
return years;
}

/* Increment array for each value between left and right. */
void incrementRange(int[] values, int left, int right) {
for (int i= left; i <= right; i++) {
values[i]++;
}
}

/* Get index of largest element in array. */
int getMaxlndex(int[] values) {
int max = 0;
for (int i= 1; i < values.length; i++) {
if (values[i] > values[max]) {
max = i;
}
}
return max;
}