From d0bcac3ab3cda944c483486681058de239f85ee3 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Fri, 1 Sep 2023 10:53:45 -0400 Subject: [PATCH] feat: add rust solution to lc problem: No.1109 --- .../1109.Corporate Flight Bookings/README.md | 27 +++++++++++++++++++ .../README_EN.md | 27 +++++++++++++++++++ .../Solution.rs | 22 +++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/README.md b/solution/1100-1199/1109.Corporate Flight Bookings/README.md index 794ba1c6e8473..18e8f5eaaeaa2 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/README.md +++ b/solution/1100-1199/1109.Corporate Flight Bookings/README.md @@ -258,6 +258,33 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn corp_flight_bookings(bookings: Vec>, n: i32) -> Vec { + let mut ans = vec![0; n as usize]; + + // Build the difference vector first + for b in &bookings { + let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + ans[l] += b[2]; + if r < n as usize - 1 { + ans[r + 1] -= b[2]; + } + } + + // Build the prefix sum vector based on the difference vector + for i in 1..n as usize { + ans[i] += ans[i - 1]; + } + + ans + } +} +``` + ### **Go** ```go diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md b/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md index 4d517a4620a8c..358c197c0bdb8 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md +++ b/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md @@ -228,6 +228,33 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn corp_flight_bookings(bookings: Vec>, n: i32) -> Vec { + let mut ans = vec![0; n as usize]; + + // Build the difference vector first + for b in &bookings { + let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + ans[l] += b[2]; + if r < n as usize - 1 { + ans[r + 1] -= b[2]; + } + } + + // Build the prefix sum vector based on the difference vector + for i in 1..n as usize { + ans[i] += ans[i - 1]; + } + + ans + } +} +``` + ### **Go** ```go diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs new file mode 100644 index 0000000000000..2089a9c488032 --- /dev/null +++ b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + #[allow(dead_code)] + pub fn corp_flight_bookings(bookings: Vec>, n: i32) -> Vec { + let mut ans = vec![0; n as usize]; + + // Build the difference vector first + for b in &bookings { + let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + ans[l] += b[2]; + if r < n as usize - 1 { + ans[r + 1] -= b[2]; + } + } + + // Build the prefix sum vector based on the difference vector + for i in 1..n as usize { + ans[i] += ans[i - 1]; + } + + ans + } +} \ No newline at end of file