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 5ab133c

Browse files
authored
feat: add rust solution to lc problem: No.0632 (doocs#1935)
No.0632.Smallest Range Covering Elements from K Lists
1 parent 4bde36c commit 5ab133c

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,49 @@ func smallestRange(nums [][]int) []int {
209209
}
210210
```
211211

212+
### **Rust**
213+
214+
```rust
215+
impl Solution {
216+
pub fn smallest_range(nums: Vec<Vec<i32>>) -> Vec<i32> {
217+
let mut t = vec![];
218+
for (i, x) in nums.iter().enumerate() {
219+
for &v in x {
220+
t.push((v, i));
221+
}
222+
}
223+
t.sort_unstable();
224+
let (mut ans, n) = (vec![-1000000, 1000000], nums.len());
225+
let mut j = 0;
226+
let mut cnt = std::collections::HashMap::new();
227+
228+
for (b, v) in t.iter() {
229+
let (b, v) = (*b, *v);
230+
if let Some(x) = cnt.get_mut(&v) {
231+
*x += 1;
232+
} else {
233+
cnt.insert(v, 1);
234+
}
235+
while cnt.len() == n {
236+
let (a, w) = t[j];
237+
let x = b - a - (ans[1] - ans[0]);
238+
if x < 0 || (x == 0 && a < ans[0]) {
239+
ans = vec![a, b];
240+
}
241+
if let Some(x) = cnt.get_mut(&w) {
242+
*x -= 1;
243+
}
244+
if cnt[&w] == 0 {
245+
cnt.remove(&w);
246+
}
247+
j += 1;
248+
}
249+
}
250+
ans
251+
}
252+
}
253+
```
254+
212255
### **...**
213256

214257
```

‎solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,49 @@ func smallestRange(nums [][]int) []int {
187187
}
188188
```
189189

190+
### **Rust**
191+
192+
```rust
193+
impl Solution {
194+
pub fn smallest_range(nums: Vec<Vec<i32>>) -> Vec<i32> {
195+
let mut t = vec![];
196+
for (i, x) in nums.iter().enumerate() {
197+
for &v in x {
198+
t.push((v, i));
199+
}
200+
}
201+
t.sort_unstable();
202+
let (mut ans, n) = (vec![-1000000, 1000000], nums.len());
203+
let mut j = 0;
204+
let mut cnt = std::collections::HashMap::new();
205+
206+
for (b, v) in t.iter() {
207+
let (b, v) = (*b, *v);
208+
if let Some(x) = cnt.get_mut(&v) {
209+
*x += 1;
210+
} else {
211+
cnt.insert(v, 1);
212+
}
213+
while cnt.len() == n {
214+
let (a, w) = t[j];
215+
let x = b - a - (ans[1] - ans[0]);
216+
if x < 0 || (x == 0 && a < ans[0]) {
217+
ans = vec![a, b];
218+
}
219+
if let Some(x) = cnt.get_mut(&w) {
220+
*x -= 1;
221+
}
222+
if cnt[&w] == 0 {
223+
cnt.remove(&w);
224+
}
225+
j += 1;
226+
}
227+
}
228+
ans
229+
}
230+
}
231+
```
232+
190233
### **...**
191234

192235
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
impl Solution {
2+
pub fn smallest_range(nums: Vec<Vec<i32>>) -> Vec<i32> {
3+
let mut t = vec![];
4+
for (i, x) in nums.iter().enumerate() {
5+
for &v in x {
6+
t.push((v, i));
7+
}
8+
}
9+
t.sort_unstable();
10+
let (mut ans, n) = (vec![-1000000, 1000000], nums.len());
11+
let mut j = 0;
12+
let mut cnt = std::collections::HashMap::new();
13+
14+
for (b, v) in t.iter() {
15+
let (b, v) = (*b, *v);
16+
if let Some(x) = cnt.get_mut(&v) {
17+
*x += 1;
18+
} else {
19+
cnt.insert(v, 1);
20+
}
21+
while cnt.len() == n {
22+
let (a, w) = t[j];
23+
let x = b - a - (ans[1] - ans[0]);
24+
if x < 0 || (x == 0 && a < ans[0]) {
25+
ans = vec![a, b];
26+
}
27+
if let Some(x) = cnt.get_mut(&w) {
28+
*x -= 1;
29+
}
30+
if cnt[&w] == 0 {
31+
cnt.remove(&w);
32+
}
33+
j += 1;
34+
}
35+
}
36+
ans
37+
}
38+
}

0 commit comments

Comments
(0)

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