[#1792]feat(MaxAverageRatio) : add C# brute force solution
This commit is contained in:
71
problems/1792-maximum-average-pass-ratio/README.md
Normal file
71
problems/1792-maximum-average-pass-ratio/README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# [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
|
||||
|
||||
## 學習筆記
|
||||
|
||||
### 今天學到什麼?
|
||||
-
|
||||
|
||||
### 遇到的困難
|
||||
-
|
||||
|
||||
### 改善方向
|
||||
-
|
||||
|
||||
### 相關題目
|
||||
- [題目編號] 題目名稱 - 相似概念
|
||||
- [題目編號] 題目名稱 - 進階版本
|
||||
|
||||
---
|
||||
**總結**: 這題的核心概念是...,適合練習...技巧。
|
130
problems/1792-maximum-average-pass-ratio/csharp/Program.cs
Normal file
130
problems/1792-maximum-average-pass-ratio/csharp/Program.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
// LeetCode 1792: Maximum Average Pass Ratio
|
||||
// 難度: Medium
|
||||
// 日期: 2025-09-01
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class Solution {
|
||||
public double MaxAverageRatioByBruteForce(int[][] classes, int extraStudents)
|
||||
{
|
||||
var n = classes.Length;
|
||||
var pass = new int[n];
|
||||
var total = new int[n];
|
||||
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
pass[i] = classes[i][0];
|
||||
total[i] = classes[i][1];
|
||||
}
|
||||
|
||||
for (int student = 0; student < extraStudents; student++)
|
||||
{
|
||||
int bestClass = -1;
|
||||
double maxInprovement = 0;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double currentAvg = CalculateAvergaeRatio(pass, total);
|
||||
|
||||
pass[i]++;
|
||||
total[i]++;
|
||||
|
||||
double newAvg = CalculateAvergaeRatio(pass, total);
|
||||
double improvement = newAvg - currentAvg;
|
||||
|
||||
pass[i]--;
|
||||
total[i]--;
|
||||
|
||||
if (improvement > maxInprovement)
|
||||
{
|
||||
maxInprovement = improvement;
|
||||
bestClass = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestClass != -1)
|
||||
{
|
||||
pass[bestClass]++;
|
||||
total[bestClass]++;
|
||||
}
|
||||
}
|
||||
|
||||
return CalculateAvergaeRatio(pass, total);
|
||||
}
|
||||
|
||||
private double CalculateAvergaeRatio(int[] pass, int[] total) {
|
||||
double sum = 0;
|
||||
for (var i = 0; i < pass.Length; i++)
|
||||
{
|
||||
sum += (double)pass[i] / total[i];
|
||||
}
|
||||
return sum / pass.Length;
|
||||
}
|
||||
}
|
||||
|
||||
// 測試程式
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
Solution solution = new Solution();
|
||||
|
||||
// TestCase
|
||||
TestCase1(solution);
|
||||
TestCase2(solution);
|
||||
}
|
||||
|
||||
static void TestCase1(Solution solution)
|
||||
{
|
||||
// Input:
|
||||
int[][] classes = new int[][]
|
||||
{
|
||||
new int[] {1, 2},
|
||||
new int[] {3, 5},
|
||||
new int[] {2, 2}
|
||||
};
|
||||
int extraStudents = 2;
|
||||
|
||||
Console.WriteLine("Test 1: ");
|
||||
Console.WriteLine($"classes: [{string.Join("], [", classes.Select(c => string.Join(", ", c)))}]");
|
||||
Console.WriteLine($"extraStudents: {extraStudents}");
|
||||
|
||||
// Expected:
|
||||
Console.WriteLine("Expect: ");
|
||||
Console.WriteLine($"output: 0.78333");
|
||||
|
||||
// Actual:
|
||||
Console.WriteLine("Actual: ");
|
||||
Console.WriteLine($"Brute Force: {solution.MaxAverageRatioByBruteForce(classes, extraStudents):F6}");
|
||||
//Console.WriteLine($"output: {solution.MaxAverageRatio(classes, extraStudents):F6}");
|
||||
}
|
||||
|
||||
|
||||
static void TestCase2(Solution solution)
|
||||
{
|
||||
// Input:
|
||||
int[][] classes = new int[][]
|
||||
{
|
||||
new int[] {2, 4},
|
||||
new int[] {3, 9},
|
||||
new int[] {4, 5},
|
||||
new int[] {2, 10}
|
||||
};
|
||||
int extraStudents = 4;
|
||||
|
||||
Console.WriteLine("Test 2: ");
|
||||
Console.WriteLine($"classes: [{string.Join("], [", classes.Select(c => string.Join(", ", c)))}]");
|
||||
Console.WriteLine($"extraStudents: {extraStudents}");
|
||||
|
||||
// Expected:
|
||||
Console.WriteLine("Expect: ");
|
||||
Console.WriteLine($"output: 0.53485");
|
||||
|
||||
// Actual:
|
||||
Console.WriteLine("Actual: ");
|
||||
Console.WriteLine($"Brute Force: {solution.MaxAverageRatioByBruteForce(classes, extraStudents):F6}");
|
||||
//Console.WriteLine($"output: {solution.MaxAverageRatio(classes, extraStudents):F6}");
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user