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 101906c

Browse files
authored
feat: add rust solution to lc problem: No.2251 (doocs#1721)
1 parent bfff868 commit 101906c

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

‎solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,59 @@ public:
221221
};
222222
```
223223

224+
### **Rust**
225+
226+
```rust
227+
use std::collections::BTreeMap;
228+
229+
impl Solution {
230+
#[allow(dead_code)]
231+
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
232+
let n = people.len();
233+
234+
// First sort the people vector based on the first item
235+
let mut people: Vec<(usize, i32)> = people
236+
.into_iter()
237+
.enumerate()
238+
.map(|x| x)
239+
.collect();
240+
241+
people.sort_by(|lhs, rhs| {
242+
lhs.1.cmp(&rhs.1)
243+
});
244+
245+
// Initialize the difference vector
246+
let mut diff = BTreeMap::new();
247+
let mut ret = vec![0; n];
248+
249+
for f in flowers {
250+
let (left, right) = (f[0], f[1]);
251+
diff
252+
.entry(left)
253+
.and_modify(|x| *x += 1)
254+
.or_insert(1);
255+
256+
diff
257+
.entry(right + 1)
258+
.and_modify(|x| *x -= 1)
259+
.or_insert(-1);
260+
}
261+
262+
let mut sum = 0;
263+
let mut i = 0;
264+
for (k, v) in diff {
265+
while i < n && people[i].1 < k {
266+
ret[people[i].0] += sum;
267+
i += 1;
268+
}
269+
sum += v;
270+
}
271+
272+
ret
273+
}
274+
}
275+
```
276+
224277
### **Go**
225278

226279
```go

‎solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,59 @@ public:
207207
};
208208
```
209209

210+
### **Rust**
211+
212+
```rust
213+
use std::collections::BTreeMap;
214+
215+
impl Solution {
216+
#[allow(dead_code)]
217+
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
218+
let n = people.len();
219+
220+
// First sort the people vector based on the first item
221+
let mut people: Vec<(usize, i32)> = people
222+
.into_iter()
223+
.enumerate()
224+
.map(|x| x)
225+
.collect();
226+
227+
people.sort_by(|lhs, rhs| {
228+
lhs.1.cmp(&rhs.1)
229+
});
230+
231+
// Initialize the difference vector
232+
let mut diff = BTreeMap::new();
233+
let mut ret = vec![0; n];
234+
235+
for f in flowers {
236+
let (left, right) = (f[0], f[1]);
237+
diff
238+
.entry(left)
239+
.and_modify(|x| *x += 1)
240+
.or_insert(1);
241+
242+
diff
243+
.entry(right + 1)
244+
.and_modify(|x| *x -= 1)
245+
.or_insert(-1);
246+
}
247+
248+
let mut sum = 0;
249+
let mut i = 0;
250+
for (k, v) in diff {
251+
while i < n && people[i].1 < k {
252+
ret[people[i].0] += sum;
253+
i += 1;
254+
}
255+
sum += v;
256+
}
257+
258+
ret
259+
}
260+
}
261+
```
262+
210263
### **Go**
211264

212265
```go
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::collections::BTreeMap;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
6+
let n = people.len();
7+
8+
// First sort the people vector based on the first item
9+
let mut people: Vec<(usize, i32)> = people
10+
.into_iter()
11+
.enumerate()
12+
.map(|x| x)
13+
.collect();
14+
15+
people.sort_by(|lhs, rhs| {
16+
lhs.1.cmp(&rhs.1)
17+
});
18+
19+
// Initialize the difference vector
20+
let mut diff = BTreeMap::new();
21+
let mut ret = vec![0; n];
22+
23+
for f in flowers {
24+
let (left, right) = (f[0], f[1]);
25+
diff
26+
.entry(left)
27+
.and_modify(|x| *x += 1)
28+
.or_insert(1);
29+
30+
diff
31+
.entry(right + 1)
32+
.and_modify(|x| *x -= 1)
33+
.or_insert(-1);
34+
}
35+
36+
let mut sum = 0;
37+
let mut i = 0;
38+
for (k, v) in diff {
39+
while i < n && people[i].1 < k {
40+
ret[people[i].0] += sum;
41+
i += 1;
42+
}
43+
sum += v;
44+
}
45+
46+
ret
47+
}
48+
}

0 commit comments

Comments
(0)

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