## Find the Town Judge LeetCode Solution

Problem Statement: Find the Town Judge Leetcode Solution: In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. …

## Rectangle Overlap LeetCode Solution

Problem Statement: Rectangle Overlap LeetCode Solution – says that An axis-aligned rectangle is represented as a list, [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left …

## Problem Statement

Maximum Population Year LeetCode Solution says that – You are given a 2D integer array `logs` where each `logs[i] = [birth`i`, death`i`]` 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` a person is counted in the year `x`‘s population if `x` is in the inclusive range `[birth`i`, death`i` - 1]`. Note that the person is not counted in the year that they die.

Return the Maximum population Year.

## Example 1:

### Input:

``` logs = [[1993,1999],[2000,2010]]
```

``` 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]]
```

``` 1960
```

### Explanation:

```
The maximum population is 2, and it had happened in years 1960 and 1970.
So the maximum population year is 1960.```

## Constraints:

• `1 <= logs.length <= 100`
• `1950 <= birth`i` < death`i` <= 2050`

## ALGORITHM –

• In order to Find the Maximum Population Year. First, we will focus on the total number population in each year by checking in each interval of the given matrix and will find the maximum count and return the year of maximum value. If the count is the same then we simply return the previous year(earliest year).

## Approach for Maximum Population Year LeetCode Solution

– First, we will create one array of size 101 because the constraints of years lie in the range 1950 to 2050.

–  after that, we will run a loop from 0 to the length of logs and will increase the count of the array at index(logs[i][o]) by 1 and decrease the count of the array at index (logs[i][1]) by 1

– again we will run a loop from 0 to the length of the array and make one variable prev count and update each element of the array by array+prev and update prev by prev = array[i].

– at last, we will run a loop and find the maximum value in the array and return that particular index(index+1950). Hence find the maximum population year.

## Code:

### Maximum Population Year Python Leetcode Solution:

```class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
arr = [0]*101
for i in range(len(logs)):

arr[logs[i][0]-1950] += 1

arr[logs[i][1]-1950] -= 1

previous = arr[0]
for i in range(1,101):
arr[i] += previous
previous = arr[i]

print(arr)
maxi = 0
ind = 0

for i in range(len(arr)):
if arr[i] > maxi:
maxi = arr[i]
ind = i + 1950
print(maxi)
return ind```

### Maximum Population Year Java Leetcode Solution:

```class Solution {
public int maximumPopulation(int[][] logs) {

int[] arr = new int[101];
for(int i = 0;i < logs.length;i++){

arr[logs[i][0]-1950] +=1;
arr[logs[i][1]-1950] -=1;

}

int prev = arr[0];
for(int i=1;i<arr.length;i++){

arr[i] += prev;
prev = arr[i];

}

int ind = 0;
int maxi = 0;

for(int i=0;i<arr.length;i++){

if(maxi < arr[i]){

maxi = arr[i];
ind = i+1950;
}
}

return ind;

}
}```

## Complexity Analysis of Maximum Population Year Leetcode Solution:

### Time Complexity

The Time Complexity of the above solution is O(n).

### Time Complexity

The Space Complexity of the above solution is O(1).

As we have made an array of length = 101. So we can consider it constant

## Binary Tree Inorder Traversal LeetCode Solution

Problem Statement: Binary Tree Inorder Traversal LeetCode solution Given the root of a binary tree, return the inorder traversal of its nodes’ values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1] Constraints: The number of nodes in …

## Detect Capital Leetcode Solution

Problem Statement: Detect Capital Leetcode Solution says that – Given a string, return true if the usage of capitals in it is right. The conditions for the right words are : All letters in this word are capitals, like “UK”. All letters in this word are not capitals, like “going”. Only …

## Maximum Population Year LeetCode Solution

Problem Statement: Maximum Population Year Leetcode Solution says that – 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 the year x‘s population if x is …

## Min Cost Climbing Stairs LeetCode Solution

Problem Statement Min Cost Climbing Stairs LeetCode Solution – An integer array cost  is given, where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with …

## Find the Town Judge LeetCode Solution

Problem Statement: Find the Town Judge LeetCode Solution – In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge and we need to find the town judge. If the town judge exists, then: The town judge trusts nobody. …

## Arranging Coins Leetcode Solution

Problem Statement The Arranging Coins LeetCode Solution – “Arranging Coins” asks you to build a staircase with these coins. The staircase consists of k rows, where ith row consists of exactly i coins. The last row of the staircase may not be complete. For the given amount of coins, return …

## Concatenation of Array LeetCode Solution

Problem Description: The Concatenation of Array Leetcode Solution: states that Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays. Return the array ans. Let’s first try to understand the problem and what it states. The problem …

Translate »