8000 Add 1854_maximum_population_year · m3xw3ll/LeetCode@020a8ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 020a8ea

Browse files
committed
Add 1854_maximum_population_year
1 parent 58bb4f0 commit 020a8ea

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [Maximum Population Year](https://leetcode.com/problems/maximum-population-year/description/)
2+
3+
You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
4+
5+
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.
6+
7+
Return the earliest year with the maximum population.
8+
9+
Example 1:
10+
```
11+
Input: logs = [[1993,1999],[2000,2010]]
12+
Output: 1993
13+
Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
14+
```
15+
Example 2:
16+
```
17+
Input: logs = [[1950,1961],[1960,1971],[1970,1981]]
18+
Output: 1960
19+
Explanation:
20+
The maximum population is 2, and it had happened in years 1960 and 1970.
21+
The earlier year between them is 1960.
22+
```
23+
Solution
24+
```python
25+
class Solution:
26+
def maximumPopulation(self, logs: List[List[int]]) -> int:
27+
d = {}
28+
29+
for log in logs:
30+
birth, death = log
31+
for i in range(birth, death):
32+
if i in d:
33+
d[i] += 1
34+
else:
35+
d[i] = 1
36+
37+
max_pop = max(d.values())
38+
return min(i for i in d if d[i] == max_pop)
39+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def maximum_population(logs):
2+
d = {}
3+
4+
for log in logs:
5+
birth, death = log
6+
for i in range(birth, death):
7+
if i in d:
8+
d[i] += 1
9+
else:
10+
d[i] = 1
11+
max_pop = max(d.values())
12+
return min(i for i in d if d[i] == max_pop)
13+
14+
15+
print(maximum_population([[2033,2034],[2039,2047],[1998,2042],[2047,2048],[2025,2029],[2005,2044],[1990,1992],[1952,1956],[1984,2014]]))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
495495
| 1844 | [Replace All Digits with Characters](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1844_replace_all_digits_with_characters.md) |
496496
| 1845 | [Seat Reservation Manager](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1845_seat_reservation_manager.md) |
497497
| 1848 | [Minimum Distance to the Target Element](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1848_minimum_distance_to_the_target_element.md) |
498+
| 1854 | [Maximum Population Year](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1854_maximum_population_year.md)
498499
| 1859 | [Sorting the Sentence](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1859_sorting_the_sentence.md) |
499500
| 1860 | [Incremental Memory Leak](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1860_incremental_memory_leak.md) |
500501
| 1863 | [Sum of All Subset XOR Totals](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/1863_sum_of_all_subset_xor_total.md) |

0 commit comments

Comments
 (0)
0