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

Commit e7d0817

Browse files
feat: add solutions to lc problem: No.1792 (#4683)
No.1792.Maximum Average Pass Ratio
1 parent f998bd8 commit e7d0817

File tree

7 files changed

+288
-9
lines changed

7 files changed

+288
-9
lines changed

‎solution/1700-1799/1792.Maximum Average Pass Ratio/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,100 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
191191
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
192192
```
193193

194+
#### TypeScript
195+
196+
```ts
197+
function maxAverageRatio(classes: number[][], extraStudents: number): number {
198+
function calcGain(a: number, b: number): number {
199+
return (a + 1) / (b + 1) - a / b;
200+
}
201+
const pq = new PriorityQueue<[number, number]>(
202+
(p, q) => calcGain(q[0], q[1]) - calcGain(p[0], p[1]),
203+
);
204+
for (const [a, b] of classes) {
205+
pq.enqueue([a, b]);
206+
}
207+
while (extraStudents-- > 0) {
208+
const item = pq.dequeue();
209+
const [a, b] = item;
210+
pq.enqueue([a + 1, b + 1]);
211+
}
212+
let ans = 0;
213+
while (!pq.isEmpty()) {
214+
const item = pq.dequeue()!;
215+
const [a, b] = item;
216+
ans += a / b;
217+
}
218+
return ans / classes.length;
219+
}
220+
```
221+
222+
#### Rust
223+
224+
```rust
225+
use std::cmp::Ordering;
226+
use std::collections::BinaryHeap;
227+
228+
impl Solution {
229+
pub fn max_average_ratio(classes: Vec<Vec<i32>>, extra_students: i32) -> f64 {
230+
struct Node {
231+
gain: f64,
232+
a: i32,
233+
b: i32,
234+
}
235+
236+
impl PartialEq for Node {
237+
fn eq(&self, other: &Self) -> bool {
238+
self.gain == other.gain
239+
}
240+
}
241+
impl Eq for Node {}
242+
243+
impl PartialOrd for Node {
244+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
245+
self.gain.partial_cmp(&other.gain)
246+
}
247+
}
248+
impl Ord for Node {
249+
fn cmp(&self, other: &Self) -> Ordering {
250+
self.partial_cmp(other).unwrap()
251+
}
252+
}
253+
254+
fn calc_gain(a: i32, b: i32) -> f64 {
255+
(a + 1) as f64 / (b + 1) as f64 - a as f64 / b as f64
256+
}
257+
258+
let n = classes.len() as f64;
259+
let mut pq = BinaryHeap::new();
260+
261+
for c in classes {
262+
let a = c[0];
263+
let b = c[1];
264+
pq.push(Node { gain: calc_gain(a, b), a, b });
265+
}
266+
267+
let mut extra = extra_students;
268+
while extra > 0 {
269+
if let Some(mut node) = pq.pop() {
270+
node.a += 1;
271+
node.b += 1;
272+
node.gain = calc_gain(node.a, node.b);
273+
pq.push(node);
274+
}
275+
extra -= 1;
276+
}
277+
278+
let mut sum = 0.0;
279+
while let Some(node) = pq.pop() {
280+
sum += node.a as f64 / node.b as f64;
281+
}
282+
283+
sum / n
284+
}
285+
}
286+
```
287+
194288
<!-- tabs:end -->
195289

196290
<!-- solution:end -->

‎solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,100 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
189189
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
190190
```
191191

192+
#### TypeScript
193+
194+
```ts
195+
function maxAverageRatio(classes: number[][], extraStudents: number): number {
196+
function calcGain(a: number, b: number): number {
197+
return (a + 1) / (b + 1) - a / b;
198+
}
199+
const pq = new PriorityQueue<[number, number]>(
200+
(p, q) => calcGain(q[0], q[1]) - calcGain(p[0], p[1]),
201+
);
202+
for (const [a, b] of classes) {
203+
pq.enqueue([a, b]);
204+
}
205+
while (extraStudents-- > 0) {
206+
const item = pq.dequeue();
207+
const [a, b] = item;
208+
pq.enqueue([a + 1, b + 1]);
209+
}
210+
let ans = 0;
211+
while (!pq.isEmpty()) {
212+
const item = pq.dequeue()!;
213+
const [a, b] = item;
214+
ans += a / b;
215+
}
216+
return ans / classes.length;
217+
}
218+
```
219+
220+
#### Rust
221+
222+
```rust
223+
use std::cmp::Ordering;
224+
use std::collections::BinaryHeap;
225+
226+
impl Solution {
227+
pub fn max_average_ratio(classes: Vec<Vec<i32>>, extra_students: i32) -> f64 {
228+
struct Node {
229+
gain: f64,
230+
a: i32,
231+
b: i32,
232+
}
233+
234+
impl PartialEq for Node {
235+
fn eq(&self, other: &Self) -> bool {
236+
self.gain == other.gain
237+
}
238+
}
239+
impl Eq for Node {}
240+
241+
impl PartialOrd for Node {
242+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
243+
self.gain.partial_cmp(&other.gain)
244+
}
245+
}
246+
impl Ord for Node {
247+
fn cmp(&self, other: &Self) -> Ordering {
248+
self.partial_cmp(other).unwrap()
249+
}
250+
}
251+
252+
fn calc_gain(a: i32, b: i32) -> f64 {
253+
(a + 1) as f64 / (b + 1) as f64 - a as f64 / b as f64
254+
}
255+
256+
let n = classes.len() as f64;
257+
let mut pq = BinaryHeap::new();
258+
259+
for c in classes {
260+
let a = c[0];
261+
let b = c[1];
262+
pq.push(Node { gain: calc_gain(a, b), a, b });
263+
}
264+
265+
let mut extra = extra_students;
266+
while extra > 0 {
267+
if let Some(mut node) = pq.pop() {
268+
node.a += 1;
269+
node.b += 1;
270+
node.gain = calc_gain(node.a, node.b);
271+
pq.push(node);
272+
}
273+
extra -= 1;
274+
}
275+
276+
let mut sum = 0.0;
277+
while let Some(node) = pq.pop() {
278+
sum += node.a as f64 / node.b as f64;
279+
}
280+
281+
sum / n
282+
}
283+
}
284+
```
285+
192286
<!-- tabs:end -->
193287

194288
<!-- solution:end -->
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::cmp::Ordering;
2+
use std::collections::BinaryHeap;
3+
4+
impl Solution {
5+
pub fn max_average_ratio(classes: Vec<Vec<i32>>, extra_students: i32) -> f64 {
6+
struct Node {
7+
gain: f64,
8+
a: i32,
9+
b: i32,
10+
}
11+
12+
impl PartialEq for Node {
13+
fn eq(&self, other: &Self) -> bool {
14+
self.gain == other.gain
15+
}
16+
}
17+
impl Eq for Node {}
18+
19+
impl PartialOrd for Node {
20+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
21+
self.gain.partial_cmp(&other.gain)
22+
}
23+
}
24+
impl Ord for Node {
25+
fn cmp(&self, other: &Self) -> Ordering {
26+
self.partial_cmp(other).unwrap()
27+
}
28+
}
29+
30+
fn calc_gain(a: i32, b: i32) -> f64 {
31+
(a + 1) as f64 / (b + 1) as f64 - a as f64 / b as f64
32+
}
33+
34+
let n = classes.len() as f64;
35+
let mut pq = BinaryHeap::new();
36+
37+
for c in classes {
38+
let a = c[0];
39+
let b = c[1];
40+
pq.push(Node {
41+
gain: calc_gain(a, b),
42+
a,
43+
b,
44+
});
45+
}
46+
47+
let mut extra = extra_students;
48+
while extra > 0 {
49+
if let Some(mut node) = pq.pop() {
50+
node.a += 1;
51+
node.b += 1;
52+
node.gain = calc_gain(node.a, node.b);
53+
pq.push(node);
54+
}
55+
extra -= 1;
56+
}
57+
58+
let mut sum = 0.0;
59+
while let Some(node) = pq.pop() {
60+
sum += node.a as f64 / node.b as f64;
61+
}
62+
63+
sum / n
64+
}
65+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function maxAverageRatio(classes: number[][], extraStudents: number): number {
2+
function calcGain(a: number, b: number): number {
3+
return (a + 1) / (b + 1) - a / b;
4+
}
5+
const pq = new PriorityQueue<[number, number]>(
6+
(p, q) => calcGain(q[0], q[1]) - calcGain(p[0], p[1]),
7+
);
8+
for (const [a, b] of classes) {
9+
pq.enqueue([a, b]);
10+
}
11+
while (extraStudents-- > 0) {
12+
const item = pq.dequeue();
13+
const [a, b] = item;
14+
pq.enqueue([a + 1, b + 1]);
15+
}
16+
let ans = 0;
17+
while (!pq.isEmpty()) {
18+
const item = pq.dequeue()!;
19+
const [a, b] = item;
20+
ans += a / b;
21+
}
22+
return ans / classes.length;
23+
}

‎solution/3200-3299/3264.Final Array State After K Multiplication Operations I/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ func (h *hp) Push(x any) {
240240

241241
```ts
242242
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
243-
const pq = new PriorityQueue({
244-
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
245-
});
243+
const pq = new PriorityQueue<number>((i, j) =>
244+
nums[i] === nums[j] ? i - j : nums[i] - nums[j],
245+
);
246+
246247
for (let i = 0; i < nums.length; ++i) {
247248
pq.enqueue(i);
248249
}

‎solution/3200-3299/3264.Final Array State After K Multiplication Operations I/README_EN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ func (h *hp) Push(x any) {
238238

239239
```ts
240240
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
241-
const pq = new PriorityQueue({
242-
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
243-
});
241+
const pq = new PriorityQueue<number>((i, j) =>
242+
nums[i] === nums[j] ? i - j : nums[i] - nums[j],
243+
);
244+
244245
for (let i = 0; i < nums.length; ++i) {
245246
pq.enqueue(i);
246247
}

‎solution/3200-3299/3264.Final Array State After K Multiplication Operations I/Solution.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
2-
const pq = new PriorityQueue({
3-
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
4-
});
2+
const pq = new PriorityQueue<number>((i, j) =>
3+
nums[i] === nums[j] ? i - j : nums[i] - nums[j],
4+
);
5+
56
for (let i = 0; i < nums.length; ++i) {
67
pq.enqueue(i);
78
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /