Files
coding-practice/problems/1792-maximum-average-pass-ratio/README.md

2.5 KiB
Raw Blame History

[1792] Maximum Average Pass Ratio

題目資訊

  • 難度: Medium
  • 標籤: Array, Greedy, Heap(Priority Queue)
  • 題目連結: LeetCode
  • 練習日期: 2025-09-01

題目描述

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.

解題思路

初步分析

  • 這題主要考察什麼概念?
    • 貪心算法,找出每次分配學生,通過率可以提升至最高
  • Pass Ratio = pass / total
  • Average Pass Ratio = sum of pass ration / number of class

解法概述

  1. 暴力解法:

    • 思路:
      • 每次分配一個學生看哪個班級提升率會最高
      • leetcode 這邊會 time limit exceeded
    • 時間複雜度O(N)
    • 空間複雜度O(N)
  2. 最佳解法:

    • 思路:
      • Priority Queue(Max Heap) 存每個班級淺在
    • 時間複雜度O(?)
    • 空間複雜度O(?)

測試案例

範例輸入輸出

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.

邊界情況

  • 1 <= classes.length <= 10^5
  • classes[i].length == 2
  • 1 <= passi <= totali <= 10^5
  • 1 <= extraStudents <= 10^5

學習筆記

今天學到什麼?

遇到的困難

改善方向

相關題目

  • [題目編號] 題目名稱 - 相似概念
  • [題目編號] 題目名稱 - 進階版本

總結: 這題的核心概念是...,適合練習...技巧。