Python3 || Beats 100% 🌟🔥|| Prefix Sum || Line sweep algorithm 💡
IntuitionThe problem asks us to find the year with the most people alive based on their birth and death years. Instead of counting the population year by year (
### **Intuition**
The problem asks us to find the year with the most people alive based on their birth and death years. Instead of counting the population year by year (which is slow), we can track how the population changes at each year using an array. This way, we calculate the population in one pass.

![image.png](https://assets.leetcode.com/users/images/9b0be1ae-20cc-4f95-bba8-c9cf56f5295e_1736063784.5807467.png)

---

### **Approach**
1. **Create an Array for Years:**
   - Use an array `people` of size 101, where each index represents the years from 1950 to 2050. For example, index `0` is 1950, index `1` is 1951, and so on.

2. **Mark Population Changes:**
   - For each log `[birth, death]`, add `1` to the year of birth (`people[birth - 1950]`) and subtract `1` from the year of death (`people[death - 1950]`). This marks when people are born and when they die.

3. **Calculate Total Population:**
   - Use a prefix sum to calculate the total population for each year. This means adding up the changes year by year so that each index in the array shows the total population for that year.

4. **Find the Year with the Most People:**
   - Find the maximum value in the array. The index of this value tells us which year had the most people. Convert it back to the actual year (`index + 1950`).

---

### **Complexity**
- **Time Complexity:**  
  - \(O(n)\) to update the array for all logs.  
  - \(O(101)\) to calculate the prefix sum and find the maximum.  
  - Overall: \(O(n)\) for large \(n\).

- **Space Complexity:**  
  - \(O(101)\) for the `people` array.  
  - This is small and treated as \(O(1)\) constant space.

---

### **Code**
```python
class Solution:
    def maximumPopulation(self, logs: List[List[int]]) -> int:
        # Create an array to track population changes
        people = [0] * 101  # 1950 to 2050 = 101 years

        # Mark population changes for each log
        for birth, death in logs:
            people[birth - 1950] += 1  # A person is born
            people[death - 1950] -= 1  # A person dies

        # Calculate the total population for each year
        for i in range(1, 101):
            people[i] += people[i - 1]

        # Find the year with the maximum population
        max_population = max(people)
        return people.index(max_population) + 1950
```

---

Happy coding 
:)
['Array', 'Prefix Sum', 'Python3']