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

76 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [1792] Maximum Average Pass Ratio
## 題目資訊
- **難度**: Medium
- **標籤**: Array, Greedy, Heap(Priority Queue)
- **題目連結**: [LeetCode](https://leetcode.com/problems/maximum-average-pass-ratio/)
- **練習日期**: 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
## 學習筆記
### 今天學到什麼?
- 第一次使用 Priority Queue
- 第一次使用 Unit Test
### 遇到的困難
- 可能要再了解 Priority Queue 更多實際用途
### 改善方向
-
### 相關題目
- [#1627](https://leetcode.com/problems/graph-connectivity-with-threshold/description/) Graph Connectivity With Threshold
- [#1728](https://leetcode.com/problems/cat-and-mouse-ii/description/) Cat and Mouse II
- [#3440](https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-ii/description/) Reschedule Meetings for Maximum Free Time II
---
**總結**:
1. 貪心策略,每次都選擇當下收益最大解
2. 練習使用Priority Queue 快速找到最大收益