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 3347ca0

Browse files
authored
feat: add rust solution to lc problem: No.1109 (doocs#1561)
1 parent 713eed0 commit 3347ca0

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎solution/1100-1199/1109.Corporate Flight Bookings/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,33 @@ public:
258258
};
259259
```
260260

261+
### **Rust**
262+
263+
```rust
264+
impl Solution {
265+
#[allow(dead_code)]
266+
pub fn corp_flight_bookings(bookings: Vec<Vec<i32>>, n: i32) -> Vec<i32> {
267+
let mut ans = vec![0; n as usize];
268+
269+
// Build the difference vector first
270+
for b in &bookings {
271+
let (l, r) = (b[0] as usize - 1, b[1] as usize - 1);
272+
ans[l] += b[2];
273+
if r < n as usize - 1 {
274+
ans[r + 1] -= b[2];
275+
}
276+
}
277+
278+
// Build the prefix sum vector based on the difference vector
279+
for i in 1..n as usize {
280+
ans[i] += ans[i - 1];
281+
}
282+
283+
ans
284+
}
285+
}
286+
```
287+
261288
### **Go**
262289

263290
```go

‎solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,33 @@ public:
228228
};
229229
```
230230

231+
### **Rust**
232+
233+
```rust
234+
impl Solution {
235+
#[allow(dead_code)]
236+
pub fn corp_flight_bookings(bookings: Vec<Vec<i32>>, n: i32) -> Vec<i32> {
237+
let mut ans = vec![0; n as usize];
238+
239+
// Build the difference vector first
240+
for b in &bookings {
241+
let (l, r) = (b[0] as usize - 1, b[1] as usize - 1);
242+
ans[l] += b[2];
243+
if r < n as usize - 1 {
244+
ans[r + 1] -= b[2];
245+
}
246+
}
247+
248+
// Build the prefix sum vector based on the difference vector
249+
for i in 1..n as usize {
250+
ans[i] += ans[i - 1];
251+
}
252+
253+
ans
254+
}
255+
}
256+
```
257+
231258
### **Go**
232259

233260
```go
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn corp_flight_bookings(bookings: Vec<Vec<i32>>, n: i32) -> Vec<i32> {
4+
let mut ans = vec![0; n as usize];
5+
6+
// Build the difference vector first
7+
for b in &bookings {
8+
let (l, r) = (b[0] as usize - 1, b[1] as usize - 1);
9+
ans[l] += b[2];
10+
if r < n as usize - 1 {
11+
ans[r + 1] -= b[2];
12+
}
13+
}
14+
15+
// Build the prefix sum vector based on the difference vector
16+
for i in 1..n as usize {
17+
ans[i] += ans[i - 1];
18+
}
19+
20+
ans
21+
}
22+
}

0 commit comments

Comments
(0)

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