Skip to content

Commit b133ca3

Browse files
committed
test: 1792 solution
py, c++, go, java
1 parent 3746ae9 commit b133ca3

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"daily": "1792",
3-
"plans": ["3663", "problems", "3664", "problems", "3665", "problems", "3668", "problems", "3669", "problems", "3670", "problems"]
3+
"plans": ["3652", "problems", "3653", "problems", "3654", "problems"]
44
}
Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
3-
3+
#include <queue>
44

55
using namespace std;
66
using json = nlohmann::json;
77

8+
struct Tp {
9+
double improvement;
10+
int pass;
11+
int total;
12+
Tp(double imp, int p, int t) : improvement(imp), pass(p), total(t) {}
13+
bool operator<(const Tp &other) const {
14+
return improvement < other.improvement;
15+
}
16+
};
17+
818
class Solution {
919
public:
10-
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
11-
20+
double maxAverageRatio(const vector<vector<int>> &classes,
21+
int extraStudents) {
22+
priority_queue<Tp> pq;
23+
for (const auto &c : classes) {
24+
int pass = c[0], total = c[1];
25+
double imp = static_cast<double>(pass + 1) / (total + 1) - static_cast<double>(pass) / total;
26+
pq.emplace(imp, pass, total);
27+
}
28+
while (extraStudents-- > 0) {
29+
auto [_, pass, total] = pq.top();
30+
pq.pop();
31+
pass++;
32+
total++;
33+
double imp = static_cast<double>(pass + 1) / (total + 1) - static_cast<double>(pass) / total;
34+
pq.emplace(imp, pass, total);
35+
}
36+
double result = 0.0;
37+
while (!pq.empty()) {
38+
auto [_, pass, total] = pq.top();
39+
pq.pop();
40+
result += static_cast<double>(pass) / total;
1241
}
42+
return result / classes.size();
43+
}
1344
};
1445

1546
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
47+
vector<string> inputArray;
48+
size_t pos = input_json_values.find('\n');
49+
while (pos != string::npos) {
50+
inputArray.push_back(input_json_values.substr(0, pos));
51+
input_json_values = input_json_values.substr(pos + 1);
52+
pos = input_json_values.find('\n');
53+
}
54+
inputArray.push_back(input_json_values);
2455

25-
Solution solution;
26-
vector<vector<int>> classes = json::parse(inputArray.at(0));
27-
int extraStudents = json::parse(inputArray.at(1));
28-
return solution.maxAverageRatio(classes, extraStudents);
56+
Solution solution;
57+
vector<vector<int>> classes = json::parse(inputArray.at(0));
58+
int extraStudents = json::parse(inputArray.at(1));
59+
return solution.maxAverageRatio(classes, extraStudents);
2960
}

problems/problems_1792/Solution.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@
77

88
public class Solution extends BaseSolution {
99
public double maxAverageRatio(int[][] classes, int extraStudents) {
10-
10+
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> Double.compare(b[0], a[0]));
11+
for (int[] c : classes) {
12+
int pass = c[0], total = c[1];
13+
double imp = (double) (pass + 1) / (total + 1) - (double) pass / total;
14+
pq.offer(new double[]{imp, pass, total});
15+
}
16+
while (extraStudents-- > 0) {
17+
double[] curr = pq.poll();
18+
curr[1]++;
19+
curr[2]++;
20+
double imp =(curr[1] + 1) / (curr[2] + 1) - curr[1] / curr[2];
21+
pq.offer(new double[]{imp, curr[1], curr[2]});
22+
}
23+
double result = 0.0;
24+
while (!pq.isEmpty()) {
25+
double[] curr = pq.poll();
26+
result += curr[1] / curr[2];
27+
}
28+
return result / classes.length;
1129
}
1230

1331
@Override

0 commit comments

Comments
 (0)