Similar Problems

Similar Problems not available

Maximum Students Taking Exam - Leetcode Solution

Companies:

LeetCode:  Maximum Students Taking Exam Leetcode Solution

Difficulty: Hard

Topics: matrix dynamic-programming bit-manipulation array  

Problem Description: Given a class schedule of n lectures, with start and end time of each lecture, and a limit k on the number of exams a student can take, what is the maximum number of students that can take exams?

Solution: This problem can be solved using binary search. We need to find the maximum number of students that can take exams by minimizing the number.

First, we need to sort the lectures according to their end time. Then, we can define a function count(exams) that will return the maximum number of students that can take exams, given that each student can take at most exams exams.

We can use a sliding window approach to count the number of exams that can be taken by each student. We keep track of the smallest start time of the next lecture that a student can attend, and the largest end time of the current exams that the student is attending. If the next lecture starts after the student's current exams have ended, the student attends the next lecture and the current exams are finished. If the student has already taken exams exams, we move to the next student.

To implement the count function, we can loop through all the lectures and count the number of exams that can be taken. If the number of exams is less than or equal to k, we return the number of students that can take exams. Otherwise, we continue with the binary search.

The binary search algorithm repeatedly calls the count function with different values of the number of exams, until the maximum number of students is found. The search range is from 1 to the number of lectures, since each student can take at most one exam per lecture.

Time Complexity: Sorting the lectures takes O(n log n) time. Each count call takes O(n) time. Therefore, the overall time complexity of the binary search is O(n log n * log n).

Space Complexity: The space complexity is O(n), since we need to store the lectures.

Here’s the implementation of the solution:

class Solution:
    def maxStudents(self, seats: List[List[str]]) -> int:
        m, n = len(seats), len(seats[0])

        def count(exams):
            res = 0
            for i in range(m):
                students = 0
                start = 0
                for j in range(n):
                    if seats[i][j] == '.':
                        if j - start >= exams:
                            students += 1
                            start = j
                    elif seats[i][j] == '#':
                        start = j + 1
                if n - start >= exams:
                    students += 1
                res += students
            return res

        l, r = 1, n
        while l <= r:
            mid = (l + r) // 2
            if count(mid) >= m:
                ans = mid
                l = mid + 1
            else:
                r = mid - 1
        return ans

In this solution, we loop through all the rows and columns of the seats matrix. We keep track of the number of students that can take exams in each row, using the variable students. We also keep track of the smallest start time of the next lecture that a student can attend, using the variable start.

We then calculate the maximum number of students that can take exams using the count function, and update the binary search range accordingly. Finally, we return the maximum number of students that can take exams.

Maximum Students Taking Exam Solution Code

1