Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10e13af

Browse files
add 1792
1 parent4ee26dc commit10e13af

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1792|[Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1792.java)||Medium|Heap|
1112
|1791|[Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1791.java)||Medium|Graph|
1213
|1790|[Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1790.java)||Easy|String|
1314
|1785|[Minimum Elements to Add to Form a Given Sum](https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1785.java)||Medium|Greedy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.PriorityQueue;
4+
5+
publicclass_1792 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* We use the change size to order the elements in the maxHeap.
9+
*/
10+
publicdoublemaxAverageRatio(int[][]classes,intextraStudents) {
11+
PriorityQueue<double[]>maxHeap =newPriorityQueue<>((a,b) -> -Double.compare(a[0],b[0]));
12+
for (int[]c :classes) {
13+
maxHeap.offer(newdouble[]{(double) (c[0] +1) / (c[1] +1) - (double)c[0] /c[1],c[0],c[1]});
14+
}
15+
while (extraStudents-- >0) {
16+
double[]curr =maxHeap.poll();
17+
curr[1]++;
18+
curr[2]++;
19+
curr[0] = (curr[1] +1) / (curr[2] +1) -curr[1] /curr[2];
20+
maxHeap.offer(curr);
21+
}
22+
doubleresult =0.0;
23+
intsize =maxHeap.size();
24+
while (!maxHeap.isEmpty()) {
25+
double[]curr =maxHeap.poll();
26+
result +=curr[1] /curr[2];
27+
}
28+
returnresult /size;
29+
}
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1792;
5+
importcom.fishercoder.solutions._3;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_1792Test {
12+
privatestatic_1792.Solution1solution1;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_1792.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
assertEquals(0.78333,solution1.maxAverageRatio(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[3,5],[2,2]"),2),0.00001);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp