Skip to content

Commit 10e13af

Browse files
add 1792
1 parent 4ee26dc commit 10e13af

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

+1
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|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1792 {
6+
public static class Solution1 {
7+
/**
8+
* We use the change size to order the elements in the maxHeap.
9+
*/
10+
public double maxAverageRatio(int[][] classes, int extraStudents) {
11+
PriorityQueue<double[]> maxHeap = new PriorityQueue<>((a, b) -> -Double.compare(a[0], b[0]));
12+
for (int[] c : classes) {
13+
maxHeap.offer(new double[]{(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+
double result = 0.0;
23+
int size = maxHeap.size();
24+
while (!maxHeap.isEmpty()) {
25+
double[] curr = maxHeap.poll();
26+
result += curr[1] / curr[2];
27+
}
28+
return result / size;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1792;
5+
import com.fishercoder.solutions._3;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1792Test {
12+
private static _1792.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1792.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
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)