Skip to content

Commit 10d45fd

Browse files
committed
feat: add solutions to lc problems: No.1791,1792
* No.1791.Find Center of Star Graph * No.1792.Maximum Average Pass Ratio
1 parent 3b5ff9e commit 10d45fd

File tree

9 files changed

+358
-2
lines changed

9 files changed

+358
-2
lines changed

solution/1700-1799/1791.Find Center of Star Graph/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:直接比较前两条边的点**
48+
49+
中心点的特点是,它与其他所有点都相连,因此只要比较前两条边的点,如果有相同的点,那么这个点就是中心点。
50+
51+
时间复杂度 $O(1)$。
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
@@ -121,6 +127,20 @@ impl Solution {
121127
}
122128
```
123129

130+
### **JavaScript**
131+
132+
```js
133+
/**
134+
* @param {number[][]} edges
135+
* @return {number}
136+
*/
137+
var findCenter = function (edges) {
138+
const [a, b] = edges[0];
139+
const [c, d] = edges[1];
140+
return a == c || a == d ? a : b;
141+
};
142+
```
143+
124144
### **...**
125145

126146
```

solution/1700-1799/1791.Find Center of Star Graph/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ impl Solution {
111111
}
112112
```
113113

114+
### **JavaScript**
115+
116+
```js
117+
/**
118+
* @param {number[][]} edges
119+
* @return {number}
120+
*/
121+
var findCenter = function (edges) {
122+
const [a, b] = edges[0];
123+
const [c, d] = edges[1];
124+
return a == c || a == d ? a : b;
125+
};
126+
```
127+
114128
### **...**
115129

116130
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @return {number}
4+
*/
5+
var findCenter = function (edges) {
6+
const [a, b] = edges[0];
7+
const [c, d] = edges[1];
8+
return a == c || a == d ? a : b;
9+
};

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

+116-1
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,137 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:优先队列(增量大根堆)**
50+
51+
假设一个班级当前的通过率为 $\frac{a}{b}$,那么如果我们将一个聪明的学生安排到此班级,那么班级的通过率就会变为 $\frac{a+1}{b+1}$。我们可以发现,通过率的增量为 $\frac{a+1}{b+1} - \frac{a}{b}$。
52+
53+
我们维护一个大根堆,堆中存储的是每个班级的通过率增量。
54+
55+
进行 `extraStudents` 次操作,每次从堆顶取出一个班级,将这个班级的人数和通过人数都加 $1$,然后将这个班级的通过率增量重新计算并放回堆中。重复这个过程,直到将所有的学生都分配完毕。
56+
57+
最后,我们将所有班级的通过率求和,然后除以班级数目,即为答案。
58+
59+
时间复杂度 $O(n\log n)$,其中 $n$ 为班级数目。
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**
5264

5365
<!-- 这里可写当前语言的特殊实现逻辑 -->
5466

5567
```python
56-
68+
class Solution:
69+
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
70+
h = [(a / b - (a + 1) / (b + 1), a, b) for a, b in classes]
71+
heapify(h)
72+
for _ in range(extraStudents):
73+
_, a, b = heappop(h)
74+
a, b = a + 1, b + 1
75+
heappush(h, (a / b - (a + 1) / (b + 1), a, b))
76+
return sum(v[1] / v[2] for v in h) / len(classes)
5777
```
5878

5979
### **Java**
6080

6181
<!-- 这里可写当前语言的特殊实现逻辑 -->
6282

6383
```java
84+
class Solution {
85+
public double maxAverageRatio(int[][] classes, int extraStudents) {
86+
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> {
87+
double x = (a[0] + 1) / (a[1] + 1) - a[0] / a[1];
88+
double y = (b[0] + 1) / (b[1] + 1) - b[0] / b[1];
89+
return Double.compare(y, x);
90+
});
91+
for (var e : classes) {
92+
pq.offer(new double[]{e[0], e[1]});
93+
}
94+
while (extraStudents-- > 0) {
95+
var e = pq.poll();
96+
double a = e[0] + 1, b = e[1] + 1;
97+
pq.offer(new double[]{a, b});
98+
}
99+
double ans = 0;
100+
while (!pq.isEmpty()) {
101+
var e = pq.poll();
102+
ans += e[0] / e[1];
103+
}
104+
return ans / classes.length;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
115+
priority_queue<tuple<double, int, int>> pq;
116+
for(auto& e : classes) {
117+
int a = e[0], b = e[1];
118+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
119+
pq.push({x, a, b});
120+
}
121+
while (extraStudents--) {
122+
auto [_, a, b] = pq.top();
123+
pq.pop();
124+
a++;
125+
b++;
126+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
127+
pq.push({x, a, b});
128+
}
129+
double ans = 0;
130+
while (pq.size()) {
131+
auto [_, a, b] = pq.top();
132+
pq.pop();
133+
ans += (double) a / b;
134+
}
135+
return ans / classes.size();
136+
}
137+
};
138+
```
64139
140+
### **Go**
141+
142+
```go
143+
func maxAverageRatio(classes [][]int, extraStudents int) float64 {
144+
pq := hp{}
145+
for _, e := range classes {
146+
a, b := e[0], e[1]
147+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
148+
heap.Push(&pq, tuple{x, a, b})
149+
}
150+
for i := 0; i < extraStudents; i++ {
151+
e := heap.Pop(&pq).(tuple)
152+
a, b := e.a+1, e.b+1
153+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
154+
heap.Push(&pq, tuple{x, a, b})
155+
}
156+
var ans float64
157+
for len(pq) > 0 {
158+
e := heap.Pop(&pq).(tuple)
159+
ans += float64(e.a) / float64(e.b)
160+
}
161+
return ans / float64(len(classes))
162+
}
163+
164+
type tuple struct {
165+
x float64
166+
a int
167+
b int
168+
}
169+
170+
type hp []tuple
171+
172+
func (h hp) Len() int { return len(h) }
173+
func (h hp) Less(i, j int) bool {
174+
a, b := h[i], h[j]
175+
return a.x > b.x
176+
}
177+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
178+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) }
179+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
65180
```
66181

67182
### **...**

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

+104-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,116 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
50+
h = [(a / b - (a + 1) / (b + 1), a, b) for a, b in classes]
51+
heapify(h)
52+
for _ in range(extraStudents):
53+
_, a, b = heappop(h)
54+
a, b = a + 1, b + 1
55+
heappush(h, (a / b - (a + 1) / (b + 1), a, b))
56+
return sum(v[1] / v[2] for v in h) / len(classes)
4957
```
5058

5159
### **Java**
5260

5361
```java
62+
class Solution {
63+
public double maxAverageRatio(int[][] classes, int extraStudents) {
64+
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> {
65+
double x = (a[0] + 1) / (a[1] + 1) - a[0] / a[1];
66+
double y = (b[0] + 1) / (b[1] + 1) - b[0] / b[1];
67+
return Double.compare(y, x);
68+
});
69+
for (var e : classes) {
70+
pq.offer(new double[]{e[0], e[1]});
71+
}
72+
while (extraStudents-- > 0) {
73+
var e = pq.poll();
74+
double a = e[0] + 1, b = e[1] + 1;
75+
pq.offer(new double[]{a, b});
76+
}
77+
double ans = 0;
78+
while (!pq.isEmpty()) {
79+
var e = pq.poll();
80+
ans += e[0] / e[1];
81+
}
82+
return ans / classes.length;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
93+
priority_queue<tuple<double, int, int>> pq;
94+
for(auto& e : classes) {
95+
int a = e[0], b = e[1];
96+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
97+
pq.push({x, a, b});
98+
}
99+
while (extraStudents--) {
100+
auto [_, a, b] = pq.top();
101+
pq.pop();
102+
a++;
103+
b++;
104+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
105+
pq.push({x, a, b});
106+
}
107+
double ans = 0;
108+
while (pq.size()) {
109+
auto [_, a, b] = pq.top();
110+
pq.pop();
111+
ans += (double) a / b;
112+
}
113+
return ans / classes.size();
114+
}
115+
};
116+
```
54117
118+
### **Go**
119+
120+
```go
121+
func maxAverageRatio(classes [][]int, extraStudents int) float64 {
122+
pq := hp{}
123+
for _, e := range classes {
124+
a, b := e[0], e[1]
125+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
126+
heap.Push(&pq, tuple{x, a, b})
127+
}
128+
for i := 0; i < extraStudents; i++ {
129+
e := heap.Pop(&pq).(tuple)
130+
a, b := e.a+1, e.b+1
131+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
132+
heap.Push(&pq, tuple{x, a, b})
133+
}
134+
var ans float64
135+
for len(pq) > 0 {
136+
e := heap.Pop(&pq).(tuple)
137+
ans += float64(e.a) / float64(e.b)
138+
}
139+
return ans / float64(len(classes))
140+
}
141+
142+
type tuple struct {
143+
x float64
144+
a int
145+
b int
146+
}
147+
148+
type hp []tuple
149+
150+
func (h hp) Len() int { return len(h) }
151+
func (h hp) Less(i, j int) bool {
152+
a, b := h[i], h[j]
153+
return a.x > b.x
154+
}
155+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
156+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) }
157+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
55158
```
56159

57160
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
4+
priority_queue<tuple<double, int, int>> pq;
5+
for(auto& e : classes) {
6+
int a = e[0], b = e[1];
7+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
8+
pq.push({x, a, b});
9+
}
10+
while (extraStudents--) {
11+
auto [_, a, b] = pq.top();
12+
pq.pop();
13+
a++;
14+
b++;
15+
double x = (double) (a + 1) / (b + 1) - (double) a / b;
16+
pq.push({x, a, b});
17+
}
18+
double ans = 0;
19+
while (pq.size()) {
20+
auto [_, a, b] = pq.top();
21+
pq.pop();
22+
ans += (double) a / b;
23+
}
24+
return ans / classes.size();
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func maxAverageRatio(classes [][]int, extraStudents int) float64 {
2+
pq := hp{}
3+
for _, e := range classes {
4+
a, b := e[0], e[1]
5+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
6+
heap.Push(&pq, tuple{x, a, b})
7+
}
8+
for i := 0; i < extraStudents; i++ {
9+
e := heap.Pop(&pq).(tuple)
10+
a, b := e.a+1, e.b+1
11+
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
12+
heap.Push(&pq, tuple{x, a, b})
13+
}
14+
var ans float64
15+
for len(pq) > 0 {
16+
e := heap.Pop(&pq).(tuple)
17+
ans += float64(e.a) / float64(e.b)
18+
}
19+
return ans / float64(len(classes))
20+
}
21+
22+
type tuple struct {
23+
x float64
24+
a int
25+
b int
26+
}
27+
28+
type hp []tuple
29+
30+
func (h hp) Len() int { return len(h) }
31+
func (h hp) Less(i, j int) bool {
32+
a, b := h[i], h[j]
33+
return a.x > b.x
34+
}
35+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
36+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) }
37+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }

0 commit comments

Comments
 (0)