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 24e3158

Browse files
feat: add solutions to lc problem: No.0786
No.0786 K-th Smallest Prime Fraction
1 parent d2e6a2b commit 24e3158

File tree

6 files changed

+283
-3
lines changed

6 files changed

+283
-3
lines changed

‎solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md‎

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<pre>
1919
<strong>输入:</strong>arr = [1,2,3,5], k = 3
2020
<strong>输出:</strong>[2,5]
21-
<strong>解释:</strong>已构造好的分数,排序后如下所示:
21+
<strong>解释:</strong>已构造好的分数,排序后如下所示:
2222
1/5, 1/3, 2/5, 1/2, 3/5, 2/3
2323
很明显第三个最小的分数是 2/5
2424
</pre>
@@ -55,15 +55,111 @@
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58-
58+
class Solution:
59+
def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:
60+
h = [(1 / y, 0, j + 1) for j, y in enumerate(arr[1:])]
61+
heapq.heapify(h)
62+
for _ in range(k - 1):
63+
_, i, j = heapq.heappop(h)
64+
if i + 1 < j:
65+
heapq.heappush(h, (arr[i + 1] / arr[j], i + 1, j))
66+
return [arr[h[0][1]], arr[h[0][2]]]
5967
```
6068

6169
### **Java**
6270

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

6573
```java
74+
class Solution {
75+
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
76+
int n = arr.length;
77+
Queue<Frac> pq = new PriorityQueue<>();
78+
for (int i = 1; i < n; i++) {
79+
pq.offer(new Frac(1, arr[i], 0, i));
80+
}
81+
for (int i = 1; i < k; i++) {
82+
Frac f = pq.poll();
83+
if (f.i + 1 < f.j) {
84+
pq.offer(new Frac(arr[f.i + 1], arr[f.j], f.i + 1, f.j));
85+
}
86+
}
87+
Frac f = pq.peek();
88+
return new int[] { f.x, f.y };
89+
}
90+
91+
static class Frac implements Comparable {
92+
int x, y, i, j;
93+
94+
public Frac(int x, int y, int i, int j) {
95+
this.x = x;
96+
this.y = y;
97+
this.i = i;
98+
this.j = j;
99+
}
100+
101+
@Override
102+
public int compareTo(Object o) {
103+
return x * ((Frac) o).y - ((Frac) o).x * y;
104+
}
105+
}
106+
}
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
type frac struct{ x, y, i, j int }
113+
type hp []frac
114+
115+
func (a hp) Len() int { return len(a) }
116+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
117+
func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y }
118+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) }
119+
func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }
120+
121+
func kthSmallestPrimeFraction(arr []int, k int) []int {
122+
n := len(arr)
123+
h := make(hp, 0, n-1)
124+
for i := 1; i < n; i++ {
125+
h = append(h, frac{1, arr[i], 0, i})
126+
}
127+
heap.Init(&h)
128+
for i := 1; i < k; i++ {
129+
f := heap.Pop(&h).(frac)
130+
if f.i+1 < f.j {
131+
heap.Push(&h, frac{arr[f.i+1], arr[f.j], f.i + 1, f.j})
132+
}
133+
}
134+
return []int{h[0].x, h[0].y}
135+
}
136+
```
66137

138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
144+
using pii = pair<int, int>;
145+
int n = arr.size();
146+
auto cmp = [&](const pii &a, const pii &b) {
147+
return arr[a.first] * arr[b.second] > arr[b.first] * arr[a.second];
148+
};
149+
priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
150+
for (int i = 1; i < n; ++i) {
151+
pq.push({0, i});
152+
}
153+
for (int i = 1; i < k; ++i) {
154+
pii f = pq.top();
155+
pq.pop();
156+
if (f.first + 1 < f.second) {
157+
pq.push({f.first + 1, f.second});
158+
}
159+
}
160+
return {arr[pq.top().first], arr[pq.top().second]};
161+
}
162+
};
67163
```
68164
69165
### **...**

‎solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md‎

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,109 @@ The third fraction is 2/5.
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:
53+
h = [(1 / y, 0, j + 1) for j, y in enumerate(arr[1:])]
54+
heapq.heapify(h)
55+
for _ in range(k - 1):
56+
_, i, j = heapq.heappop(h)
57+
if i + 1 < j:
58+
heapq.heappush(h, (arr[i + 1] / arr[j], i + 1, j))
59+
return [arr[h[0][1]], arr[h[0][2]]]
5260
```
5361

5462
### **Java**
5563

5664
```java
65+
class Solution {
66+
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
67+
int n = arr.length;
68+
Queue<Frac> pq = new PriorityQueue<>();
69+
for (int i = 1; i < n; i++) {
70+
pq.offer(new Frac(1, arr[i], 0, i));
71+
}
72+
for (int i = 1; i < k; i++) {
73+
Frac f = pq.poll();
74+
if (f.i + 1 < f.j) {
75+
pq.offer(new Frac(arr[f.i + 1], arr[f.j], f.i + 1, f.j));
76+
}
77+
}
78+
Frac f = pq.peek();
79+
return new int[] { f.x, f.y };
80+
}
81+
82+
static class Frac implements Comparable {
83+
int x, y, i, j;
84+
85+
public Frac(int x, int y, int i, int j) {
86+
this.x = x;
87+
this.y = y;
88+
this.i = i;
89+
this.j = j;
90+
}
91+
92+
@Override
93+
public int compareTo(Object o) {
94+
return x * ((Frac) o).y - ((Frac) o).x * y;
95+
}
96+
}
97+
}
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
type frac struct{ x, y, i, j int }
104+
type hp []frac
105+
106+
func (a hp) Len() int { return len(a) }
107+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
108+
func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y }
109+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) }
110+
func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }
111+
112+
func kthSmallestPrimeFraction(arr []int, k int) []int {
113+
n := len(arr)
114+
h := make(hp, 0, n-1)
115+
for i := 1; i < n; i++ {
116+
h = append(h, frac{1, arr[i], 0, i})
117+
}
118+
heap.Init(&h)
119+
for i := 1; i < k; i++ {
120+
f := heap.Pop(&h).(frac)
121+
if f.i+1 < f.j {
122+
heap.Push(&h, frac{arr[f.i+1], arr[f.j], f.i + 1, f.j})
123+
}
124+
}
125+
return []int{h[0].x, h[0].y}
126+
}
127+
```
57128

129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
135+
using pii = pair<int, int>;
136+
int n = arr.size();
137+
auto cmp = [&](const pii &a, const pii &b) {
138+
return arr[a.first] * arr[b.second] > arr[b.first] * arr[a.second];
139+
};
140+
priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
141+
for (int i = 1; i < n; ++i) {
142+
pq.push({0, i});
143+
}
144+
for (int i = 1; i < k; ++i) {
145+
pii f = pq.top();
146+
pq.pop();
147+
if (f.first + 1 < f.second) {
148+
pq.push({f.first + 1, f.second});
149+
}
150+
}
151+
return {arr[pq.top().first], arr[pq.top().second]};
152+
}
153+
};
58154
```
59155
60156
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
4+
using pii = pair<int, int>;
5+
int n = arr.size();
6+
auto cmp = [&](const pii &a, const pii &b) {
7+
return arr[a.first] * arr[b.second] > arr[b.first] * arr[a.second];
8+
};
9+
priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
10+
for (int i = 1; i < n; ++i) {
11+
pq.push({0, i});
12+
}
13+
for (int i = 1; i < k; ++i) {
14+
pii f = pq.top();
15+
pq.pop();
16+
if (f.first + 1 < f.second) {
17+
pq.push({f.first + 1, f.second});
18+
}
19+
}
20+
return {arr[pq.top().first], arr[pq.top().second]};
21+
}
22+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type frac struct{ x, y, i, j int }
2+
type hp []frac
3+
4+
func (a hp) Len() int { return len(a) }
5+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
6+
func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y }
7+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) }
8+
func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }
9+
10+
func kthSmallestPrimeFraction(arr []int, k int) []int {
11+
n := len(arr)
12+
h := make(hp, 0, n-1)
13+
for i := 1; i < n; i++ {
14+
h = append(h, frac{1, arr[i], 0, i})
15+
}
16+
heap.Init(&h)
17+
for i := 1; i < k; i++ {
18+
f := heap.Pop(&h).(frac)
19+
if f.i+1 < f.j {
20+
heap.Push(&h, frac{arr[f.i+1], arr[f.j], f.i + 1, f.j})
21+
}
22+
}
23+
return []int{h[0].x, h[0].y}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
3+
int n = arr.length;
4+
Queue<Frac> pq = new PriorityQueue<>();
5+
for (int i = 1; i < n; i++) {
6+
pq.offer(new Frac(1, arr[i], 0, i));
7+
}
8+
for (int i = 1; i < k; i++) {
9+
Frac f = pq.poll();
10+
if (f.i + 1 < f.j) {
11+
pq.offer(new Frac(arr[f.i + 1], arr[f.j], f.i + 1, f.j));
12+
}
13+
}
14+
Frac f = pq.peek();
15+
return new int[] { f.x, f.y };
16+
}
17+
18+
static class Frac implements Comparable {
19+
int x, y, i, j;
20+
21+
public Frac(int x, int y, int i, int j) {
22+
this.x = x;
23+
this.y = y;
24+
this.i = i;
25+
this.j = j;
26+
}
27+
28+
@Override
29+
public int compareTo(Object o) {
30+
return x * ((Frac) o).y - ((Frac) o).x * y;
31+
}
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:
3+
h = [(1 / y, 0, j + 1) for j, y in enumerate(arr[1:])]
4+
heapq.heapify(h)
5+
for _ in range(k - 1):
6+
_, i, j = heapq.heappop(h)
7+
if i + 1 < j:
8+
heapq.heappush(h, (arr[i + 1] / arr[j], i + 1, j))
9+
return [arr[h[0][1]], arr[h[0][2]]]

0 commit comments

Comments
(0)

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