Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Average Pass Ratio

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1792" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes, where classes[i] = [passi, totali]. You know beforehand that in the ith class, there are totali total students, but only passi number of students will pass the exam. You are also given an integer extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes. The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes. Return the maximum possible average pass ratio after assigning the extraStudents students. Answers within 10-5 of the actual answer will be accepted. Example 1: Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2 Output: 0.78333 Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333. Example 2: Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4 Output: 0.53485 Constraints: 1 <= classes.length <= 105 classes[i].length == 2 1 <= passi <= totali <= 105 1 <= extraStudents <= 105

Explanation

Here's a breakdown of the approach and the Python code:

  • Key Idea: The core idea is to greedily assign extra students to the class that yields the maximum increase in the average pass ratio. This can be achieved by using a max-heap (priority queue) to store the potential increase in pass ratio for each class if one student is added.

  • Greedy Assignment: Iteratively, we extract the class with the highest potential increase from the heap, add a student to it, and update the heap with the new potential increase for that class.

  • Heap Optimization: The heap stores the change in pass ratio, not the pass ratio itself. This is crucial for efficient greedy assignment.

  • Complexity:

    • Runtime: O(n + k log n), where n is the number of classes and k is extraStudents. The O(n) is for the initial heap construction, and O(k log n) is for extracting from and inserting into the heap k times.
    • Storage: O(n) for the heap.

Code

    import heapq

def maxAverageRatio(classes, extraStudents):
    """
    Calculates the maximum possible average pass ratio after assigning extraStudents.

    Args:
        classes: A list of lists, where classes[i] = [passi, totali].
        extraStudents: The number of extra students to assign.

    Returns:
        The maximum possible average pass ratio.
    """

    # Calculate the increase in ratio if we add one student to a class
    def calculate_increase(passi, totali):
        return (passi + 1) / (totali + 1) - passi / totali

    # Create a max-heap to store the potential increase in pass ratio for each class
    heap = []
    for passi, totali in classes:
        increase = calculate_increase(passi, totali)
        heapq.heappush(heap, (-increase, passi, totali))  # Negate to make it a max-heap

    # Assign extra students greedily to the class with the highest potential increase
    for _ in range(extraStudents):
        increase, passi, totali = heapq.heappop(heap)
        passi += 1
        totali += 1
        increase = calculate_increase(passi, totali)
        heapq.heappush(heap, (-increase, passi, totali))

    # Calculate the average pass ratio
    total_ratio = 0
    for _, passi, totali in heap:
        total_ratio += passi / totali

    return total_ratio / len(classes)

More from this blog

C

Chatmagic blog

2894 posts