Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Problem 1792. Maximum Average Pass Ratio #2270

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [1759. Count Number of Homogenous Substrings](https://leetcode.com/problems/count-number-of-homogenous-substrings) | Medium | [C](./old-problems/1759/c/solution.c) [C++](./old-problems/1759/solution.cpp) |
| [1770. Maximum Score from Performing Multiplication Operations](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/) | Hard | [C++](./old-problems/1770/solution.cpp) |
| [1791. Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/) | Easy | [C](./old-problems/1791/c/solution.c) [C++](./old-problems/1791/solution.cpp) |
| [1792. Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | Medium | [C++](./old-problems/1792/solution.cpp) |
| [1793. Maximum Score of a Good Subarray](https://leetcode.com/problems/maximum-score-of-a-good-subarray) | Hard | [C](./old-problems/1793/c/solution.c) [C++](./old-problems/1793/solution.cpp) |
| [1813. Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/) | Medium | [C](./old-problems/1813/c/solution.c) [C++](./old-problems/1813/solution.cpp) |
| [1814. Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | Medium | [C++](./problems/1814/solution.cpp) |
Expand Down
2 changes: 2 additions & 0 deletions old-problems/1792/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
get_dir_name(id)
add_problem_test(test-${id} test.yaml)
34 changes: 34 additions & 0 deletions old-problems/1792/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <queue>
#include <tuple>
#include <vector>

class Solution {
public:
double maxAverageRatio(
std::vector<std::vector<int>>& classes, int extraStudents) {
std::priority_queue<std::tuple<double, int, int>> ratios{};
for (const auto& cls : classes) {
const double a = cls[0], b = cls[1];
ratios.push({(b - a) / (b * b + b), cls[0], cls[1]});
}

while (extraStudents > 0) {
auto ratio = ratios.top();
ratios.pop();

const double a = ++std::get<1>(ratio), b = ++std::get<2>(ratio);
std::get<0>(ratio) = (b - a) / (b * b + b);

ratios.push(ratio);
--extraStudents;
}

double sum{0.0};
while (!ratios.empty()) {
sum += (double)std::get<1>(ratios.top()) / (double)std::get<2>(ratios.top());
ratios.pop();
}

return sum / classes.size();
}
};
24 changes: 24 additions & 0 deletions old-problems/1792/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: 1792. Maximum Average Pass Ratio

types:
inputs:
classes: std::vector<std::vector<int>>
extraStudents: int
output: double

solutions:
cpp:
function: maxAverageRatio

test_cases:
example_1:
inputs:
classes: [[1, 2], [3, 5], [2, 2]]
extraStudents: 2
output: 0.78333333333333333

example_2:
inputs:
classes: [[2, 4], [3, 9], [4, 5], [2, 10]]
extraStudents: 4
output: 0.53484848484848491
Loading