-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMostLivingPeople.cs
66 lines (57 loc) · 2.24 KB
/
MostLivingPeople.cs
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
using System;
using System.Collections.Generic;
namespace PracticeQuestionsSharp.Exercises.Numbers
{
public static class MostLivingPeople
{
//Given a list of people with their birth and death years, compute the year with most people alive.
// Where int[n][0] = birth year & int[n][1] = death year
public static int GetYearWithMostLivingPeople(int[][] birthAndDeathYears)
{
List<int> birthYears = new List<int>();
List<int> deathYears = new List<int>();
int maxYear = -1;
int maxPeople = -1;
int aliveCount = 0;
//Iterate through birth and death years, inserting them into lists in order
foreach (int[] bd in birthAndDeathYears)
{
int birthIndex = birthYears.BinarySearch(bd[0]);
int deathIndex = deathYears.BinarySearch(bd[1]);
if (birthIndex < 0) birthIndex = ~birthIndex;
if (deathIndex < 0) deathIndex = ~deathIndex;
birthYears.Insert(birthIndex, bd[0]);
deathYears.Insert(deathIndex, bd[1]);
//If binary search doesn't find the element, it returns the bitwise complement of index of the next element that is larger
//That's pretty neat
}
//Loop through birth and death years (now sorted) and increment/decrement keeping track of the max
int i = 0, j = 0;
while (i < birthYears.Count)
{
if (birthYears[i] < deathYears[j])
{
aliveCount++;
if (aliveCount > maxPeople)
{
maxYear = birthYears[i];
maxPeople = aliveCount;
}
i++;
}
else if (birthYears[i] == deathYears[j])
{
i++;
j++;
}
else //birthYears[i] > deathYears[i]
{
aliveCount--;
j++;
}
}
Console.WriteLine($"Max year - {maxYear}, Max people - {maxPeople}");
return maxYear;
}
}
}