Palindrome Number LeetCode Solution

Problem Statement Palindrome Number LeetCode Solution says that – Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not.   Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right …

Read more

Vertical Order Traversal of Binary Tree LeetCode Solution

Problem Statement Vertical Order Traversal of Binary Tree LeetCode Solution says – Given the root of a binary tree, calculate the vertical order traversal of the binary tree. For each node at position (row, col), its left and right children will be at positions (row + 1, col – 1) and (row + 1, col + 1) respectively. …

Read more

Top K Frequent Elements LeetCode Solution

Problem Statement Top K Frequent Elements LeetCode Solution Says that – Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] …

Read more

Total Hamming Distance LeetCode Solution

Problem Statement: Total Hamming Distance LeetCode Solution: Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums. The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Example 1: Input: nums = [4,14,2] Output: 6 Explanation: In binary representation, …

Read more

Shortest Unsorted Continuous Subarray LeetCode Solution

Problem Statement Shortest Unsorted Continuous Subarray LeetCode Solution says that – Given an integer array nums, you have to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order. Return the length of the shortest subarray. Example 1: …

Read more

Sum Root to Leaf Numbers LeetCode Solution

Problem Statement Sum Root to Leaf Numbers LeetCode Solution says – You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test …

Read more

Score of Parenthesis LeetCode Solution

Problem Statement The score of Parenthesis LeetCode Solution says – Given a balanced parentheses string s and return the maximum score. The score of a balanced parenthesis string is based on the following rules: “()” has score 1. AB has score A + B, where A and B are balanced parenthesis strings. (A) has score 2 * A, where A is a …

Read more

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 …

Read more

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 a person is counted in the 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 Maximum population Year.

 

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.
So the maximum population year is 1960.

 

Constraints:

  • 1 <= logs.length <= 100
  • 1950 <= birthi < deathi <= 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.

Maximum Population Year Leetcode Solution

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

 

 

 

 

 

 

Translate »