1792. Maximum Average Pass Ratio

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

Rust Solution

struct Solution;
use std::cmp::Ordering;
use std::collections::BinaryHeap;

#[derive(PartialEq, Eq, Debug)]
struct Class {
    pass: i32,
    total: i32,
}

impl Class {
    fn new(pass: i32, total: i32) -> Self {
        Class { pass, total }
    }

    fn add(&mut self) {
        self.pass += 1;
        self.total += 1;
    }

    fn inc(&self) -> f64 {
        (self.pass + 1) as f64 / (self.total + 1) as f64 - self.pass as f64 / self.total as f64
    }
}

impl Ord for Class {
    fn cmp(&self, other: &Self) -> Ordering {
        self.inc().partial_cmp(&other.inc()).unwrap()
    }
}

impl PartialOrd for Class {
    fn partial_cmp(&self, other: &Class) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Solution {
    fn max_average_ratio(classes: Vec<Vec<i32>>, extra_students: i32) -> f64 {
        let n = classes.len();
        let mut queue: BinaryHeap<Class> = BinaryHeap::new();
        for i in 0..n {
            let class = Class::new(classes[i][0], classes[i][1]);
            queue.push(class);
        }
        for _ in 0..extra_students {
            if let Some(mut top) = queue.pop() {
                top.add();
                queue.push(top);
            }
        }
        let mut sum = 0.0;
        while let Some(top) = queue.pop() {
            sum += top.pass as f64 / top.total as f64;
        }
        sum / n as f64
    }
}

#[test]
fn test() {
    use assert_approx_eq::assert_approx_eq;
    let classes = vec_vec_i32![[1, 2], [3, 5], [2, 2]];
    let extra_students = 2;
    let res = 0.78333;
    assert_approx_eq!(
        Solution::max_average_ratio(classes, extra_students),
        res,
        1.0e-5
    );
    let classes = vec_vec_i32![[2, 4], [3, 9], [4, 5], [2, 10]];
    let extra_students = 4;
    let res = 0.53485;
    assert_approx_eq!(
        Solution::max_average_ratio(classes, extra_students),
        res,
        1.0e-5
    );
}

Having problems with this solution? Click here to submit an issue on github.