LeetCode 618: Students Report By Geography Solution

Master LeetCode problem 618 (Students Report By Geography), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

618. Students Report By Geography

Problem Explanation

Explanation:

To solve this problem, we can use a binary search approach. We can start by sorting the given list of student scores. Then, for each query, we can perform a binary search to find the number of students with scores between the given range.

Here are the steps:

  1. Sort the list of student scores in ascending order.
  2. For each query, use binary search to find the index of the first student with a score greater than or equal to the lower bound, and the index of the last student with a score less than or equal to the upper bound.
  3. Calculate the number of students within the range by subtracting the indexes found in step 2 and adding 1.

Time complexity analysis:

  • Sorting the list of student scores takes O(nlogn) time.
  • Processing each query using binary search takes O(logn) time.
  • Therefore, the overall time complexity is O(nlogn + qlogn), where n is the number of students and q is the number of queries.

Space complexity:

  • We only need a constant amount of extra space for variables, so the space complexity is O(1).

:

Solution Code

class Solution {
    public List<Integer> countStudents(int[] heights, int[][] people) {
        Arrays.sort(heights);
        List<Integer> result = new ArrayList<>();
        
        for (int[] p : people) {
            int low = 0, high = heights.length - 1;
            int start = -1, end = -1;
            
            while (low <= high) {
                int mid = low + (high - low) / 2;
                if (heights[mid] >= p[0]) {
                    start = mid;
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }
            
            low = 0;
            high = heights.length - 1;
            
            while (low <= high) {
                int mid = low + (high - low) / 2;
                if (heights[mid] <= p[1]) {
                    end = mid;
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
            }
            
            result.add(end - start + 1);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 618 (Students Report By Geography)?

This page provides optimized solutions for LeetCode problem 618 (Students Report By Geography) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 618 (Students Report By Geography)?

The time complexity for LeetCode 618 (Students Report By Geography) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 618 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 618 in Java, C++, or Python.

Back to LeetCode Solutions