You are given a 2D integer array logs
where each logs[i] = [birthi, deathi]
indicates the birth and death years of the ith
person.
The population of some year x
is the number of people alive during that year. The ith
person is counted in year x
's population if x
is in the inclusive range [birthi, deathi - 1]
. Note that the person is not counted in the year that they die.
Return the earliest year with the maximum population.
Example 1:
Input: logs = [[1993,1999],[2000,2010]] Output: 1993 Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
Example 2:
Input: logs = [[1950,1961],[1960,1971],[1970,1981]] Output: 1960 Explanation: The maximum population is 2, and it had happened in years 1960 and 1970. The earlier year between them is 1960.
Constraints:
1 <= logs.length <= 100
1950 <= birthi < deathi <= 2050
program main
implicit none
integer, parameter :: dp = selected_real_kind(15)
integer, dimension(:), allocatable :: logs
integer :: n, i, j, k, m, max_pop, min_year, max_year, pop
logical :: found
! read input
read(*,*) n
allocate(logs(n))
do i = 1, n
read(*,*) logs(i)%birth, logs(i)%death
end do
! sort by birth year
call sort(logs)
! find earliest year with maximum population
max_pop = 0
min_year = huge(min_year)
max_year = -huge(max_year)
do i = 1, n
pop = 0
found = .false.
do j = 1, n
if (logs(j)%birth >= logs(i)%birth .and. &
logs(j)%death <= logs(i)%death) then
pop = pop + 1
found = .true.
end if
end do
if (found) then
if (pop > max_pop) then
max_pop = pop
min_year = logs(i)%birth
max_year = logs(i)%death
else if (pop == max_pop) then
if (logs(i)%birth < min_year) then
min_year = logs(i)%birth
end if
if (logs(i)%death > max_year) then
max_year = logs(i)%death
end if
end if
end if
end do
write(*,'(I0)') min_year
contains
subroutine sort(arr)
implicit none
integer, dimension(:), intent(inout) :: arr
integer :: i, j, temp
do i = 1, size(arr)-1
do j = i+1, size(arr)
if (arr(i)%birth > arr(j)%birth) then
temp = arr(i)%birth
arr(i)%birth = arr(j)%birth
arr(j)%birth = temp
temp = arr(i)%death
arr(i)%death = arr(j)%death
arr(j)%death = temp
end if
end do
end do
end subroutine sort
end program main
temp.f95:14:27: 14 | read(*,*) logs(i)%birth, logs(i)%death | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:28:25: 28 | if (logs(j)%birth >= logs(i)%birth .and. & | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:32:15: 32 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:37:36: 37 | min_year = logs(i)%birth | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:38:36: 38 | max_year = logs(i)%death | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:40:29: 40 | if (logs(i)%birth < min_year) then | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:41:40: 41 | min_year = logs(i)%birth | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:43:29: 43 | if (logs(i)%death > max_year) then | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:44:40: 44 | max_year = logs(i)%death | 1 Error: Unexpected โ%โ for nonderived-type variable โlogsโ at (1) temp.f95:46:15: 46 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:47:11: 47 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:61:28: 61 | if (arr(i)%birth > arr(j)%birth) then | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:62:35: 62 | temp = arr(i)%birth | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:63:28: 63 | arr(i)%birth = arr(j)%birth | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:64:28: 64 | arr(j)%birth = temp | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:65:35: 65 | temp = arr(i)%death | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:66:28: 66 | arr(i)%death = arr(j)%death | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:67:28: 67 | arr(j)%death = temp | 1 Error: Unexpected โ%โ for nonderived-type variable โarrโ at (1) temp.f95:68:19: 68 | end if | 1 Error: Expecting END DO statement at (1)
module PopulationCounter
contains
function get_earliest_year_with_max_population(logs) result(earliest_year)
integer, intent(in) :: logs(:, :)
integer :: birth(size(logs, 1)), death(size(logs, 1)), population(0:2050)
integer :: i, j, max_population, earliest_year
! Initialize the birth and death years
do i = 1, size(logs, 1)
birth(i) = logs(i, 1)
death(i) = logs(i, 2)
end do
! Initialize the population array
population = 0
! Loop through each year and increment the population
do i = 1950, 2050
do j = 1, size(logs, 1)
if (i >= birth(j) .and. i <= death(j)) then
population(i) = population(i) + 1
end if
end do
end do
! Find the earliest year with the maximum population
max_population = maxval(population)
earliest_year = minval(pack((/ (i, i = 1950, 2050) /), population == max_population))
end function get_earliest_year_with_max_population
end module PopulationCounter
! Test the function with the given examples
program test_population_counter
use PopulationCounter
implicit none
integer, parameter :: logs(2, 2) = reshape([1993, 1999, 2000, 2010], shape(logs))
integer, parameter :: expected_earliest_year = 1993
integer :: earliest_year
earliest_year = get_earliest_year_with_max_population(logs)
if (earliest_year == expected_earliest_year) then
print *, "Test 1: Passed"
else
print *, "Test 1: Failed"
end if
integer, parameter :: logs2(3, 2) = reshape([1950, 1961, 1960, 1971, 1970, 1981], shape(logs2))
integer, parameter :: expected_earliest_year2 = 1960
earliest_year = get_earliest_year_with_max_population(logs2)
if (earliest_year == expected_earliest_year2) then
print *, "Test 2: Passed"
else
print *, "Test 2: Failed"
end if
end program test_population_counter
temp.f95:28:36: 28 | earliest_year = minval(pack((/ (i, i = 1950, 2050) /), population == max_population)) | 1 Error: Different shape for arguments 'array' and 'mask' for intrinsic 'pack' at (1) on dimension 1 (101 and 2051) temp.f95:34:9: 34 | use PopulationCounter | 1 Fatal Error: Cannot open module file โpopulationcounter.modโ for reading at (1): No such file or directory compilation terminated.
def maxAliveYear(logs):
years = [0] * 101
for log in logs:
years[log[0] - 1950] += 1
years[log[1] - 1950] -= 1
max_population = years[0]
max_year = 1950
for i in range(1, 101):
years[i] += years[i - 1]
if years[i] > max_population:
max_population = years[i]
max_year = i + 1950
return max_year
The algorithm iterates through the logs and increments the birth year and decrements the death year in the years array. Then, starting from the first year, it accumulates the population for each year by summing up the previous year's population with the current year's population. After that, it checks which year has the maximum population and returns that year.
int maxAliveYear(vector<vector<int>>& logs) {
vector<int> years(101, 0);
for (vector<int> log : logs) {
years[log[0] - 1950]++;
years[log[1] - 1950]--;
}
int max_population = years[0];
int max_year = 1950;
for (int i = 1; i < 101; i++) {
years[i] += years[i - 1];
if (years[i] > max_population) {
max_population = years[i];
max_year = i + 1950;
}
}
return max_year;
}
The algorithm iterates through the logs and increments the birth year and decrements the death year in the years array. Then, starting from the first year, it accumulates the population for each year by summing up the previous year's population with the current year's population. After that, it checks which year has the maximum population and returns that year.