Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.2251 #1721

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,59 @@ public:
};
```

### **Rust**

```rust
use std::collections::BTreeMap;

impl Solution {
#[allow(dead_code)]
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
let n = people.len();

// First sort the people vector based on the first item
let mut people: Vec<(usize, i32)> = people
.into_iter()
.enumerate()
.map(|x| x)
.collect();

people.sort_by(|lhs, rhs| {
lhs.1.cmp(&rhs.1)
});

// Initialize the difference vector
let mut diff = BTreeMap::new();
let mut ret = vec![0; n];

for f in flowers {
let (left, right) = (f[0], f[1]);
diff
.entry(left)
.and_modify(|x| *x += 1)
.or_insert(1);

diff
.entry(right + 1)
.and_modify(|x| *x -= 1)
.or_insert(-1);
}

let mut sum = 0;
let mut i = 0;
for (k, v) in diff {
while i < n && people[i].1 < k {
ret[people[i].0] += sum;
i += 1;
}
sum += v;
}

ret
}
}
```

### **Go**

```go
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,59 @@ public:
};
```

### **Rust**

```rust
use std::collections::BTreeMap;

impl Solution {
#[allow(dead_code)]
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
let n = people.len();

// First sort the people vector based on the first item
let mut people: Vec<(usize, i32)> = people
.into_iter()
.enumerate()
.map(|x| x)
.collect();

people.sort_by(|lhs, rhs| {
lhs.1.cmp(&rhs.1)
});

// Initialize the difference vector
let mut diff = BTreeMap::new();
let mut ret = vec![0; n];

for f in flowers {
let (left, right) = (f[0], f[1]);
diff
.entry(left)
.and_modify(|x| *x += 1)
.or_insert(1);

diff
.entry(right + 1)
.and_modify(|x| *x -= 1)
.or_insert(-1);
}

let mut sum = 0;
let mut i = 0;
for (k, v) in diff {
while i < n && people[i].1 < k {
ret[people[i].0] += sum;
i += 1;
}
sum += v;
}

ret
}
}
```

### **Go**

```go
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::collections::BTreeMap;

impl Solution {
#[allow(dead_code)]
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
let n = people.len();

// First sort the people vector based on the first item
let mut people: Vec<(usize, i32)> = people
.into_iter()
.enumerate()
.map(|x| x)
.collect();

people.sort_by(|lhs, rhs| {
lhs.1.cmp(&rhs.1)
});

// Initialize the difference vector
let mut diff = BTreeMap::new();
let mut ret = vec![0; n];

for f in flowers {
let (left, right) = (f[0], f[1]);
diff
.entry(left)
.and_modify(|x| *x += 1)
.or_insert(1);

diff
.entry(right + 1)
.and_modify(|x| *x -= 1)
.or_insert(-1);
}

let mut sum = 0;
let mut i = 0;
for (k, v) in diff {
while i < n && people[i].1 < k {
ret[people[i].0] += sum;
i += 1;
}
sum += v;
}

ret
}
}