Skip to content

Commit 2ae1906

Browse files
authored
feat: add rust solution to lc problem: No.0304 (#1127)
1 parent 4ad4708 commit 2ae1906

File tree

3 files changed

+198
-1
lines changed

3 files changed

+198
-1
lines changed

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,68 @@ public:
165165
*/
166166
```
167167
168+
### **Rust**
169+
170+
```rust
171+
/**
172+
* Your NumMatrix object will be instantiated and called as such:
173+
* let obj = NumMatrix::new(matrix);
174+
* let ret_1: i32 = obj.sum_region(row1, col1, row2, col2);
175+
*/
176+
177+
struct NumMatrix {
178+
// Of size (N + 1) * (M + 1)
179+
prefix_vec: Vec<Vec<i32>>,
180+
n: usize,
181+
m: usize,
182+
is_initialized: bool,
183+
ref_vec: Vec<Vec<i32>>,
184+
}
185+
186+
187+
/**
188+
* `&self` means the method takes an immutable reference.
189+
* If you need a mutable reference, change it to `&mut self` instead.
190+
*/
191+
impl NumMatrix {
192+
193+
fn new(matrix: Vec<Vec<i32>>) -> Self {
194+
NumMatrix {
195+
prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1],
196+
n: matrix.len(),
197+
m: matrix[0].len(),
198+
is_initialized: false,
199+
ref_vec: matrix,
200+
}
201+
}
202+
203+
fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 {
204+
if !self.is_initialized {
205+
self.initialize_prefix_vec();
206+
}
207+
// Since i32 will let `rustc` complain, just make it happy
208+
let row1: usize = row1 as usize;
209+
let col1: usize = col1 as usize;
210+
let row2: usize = row2 as usize;
211+
let col2: usize = col2 as usize;
212+
// Return the value in O(1)
213+
self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1]
214+
- self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1]
215+
}
216+
217+
fn initialize_prefix_vec(&mut self) {
218+
// Initialize the prefix sum vector
219+
for i in 0..self.n {
220+
for j in 0..self.m {
221+
self.prefix_vec[i + 1][j + 1] =
222+
self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j];
223+
}
224+
}
225+
self.is_initialized = true;
226+
}
227+
}
228+
```
229+
168230
### **Go**
169231

170232
```go

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,23 @@ numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
5252

5353
## Solutions
5454

55-
Dynamic programming - 2D preSum.
55+
We use $s[i + 1][j + 1]$ to represent the sum of all elements in the upper-left part up to the $i$-th row and $j$-th column, where the indices $i$ and $j$ both start from $0$.
56+
57+
We can derive the following prefix sum formula:
58+
59+
$$
60+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]
61+
$$
62+
63+
The sum of the elements in the rectangle with $(x_1, y_1)$ and $(x_2, y_2)$ as its upper-left and bottom-right corners respectively, is:
64+
65+
$$
66+
s[x_2 + 1][y_2 + 1] - s[x_2 + 1][y_1] - s[x_1][y_2 + 1] + s[x_1][y_1]
67+
$$
68+
69+
We preprocess the prefix sum array $s$ in the initialization method, and directly return the result of the above formula in the query method.
70+
71+
The time complexity for initialization is $O(m \times n)$, and the time complexity for query is $O(1)$.
5672

5773
<!-- tabs:start -->
5874

@@ -140,6 +156,68 @@ public:
140156
*/
141157
```
142158
159+
### **Rust**
160+
161+
```rust
162+
/**
163+
* Your NumMatrix object will be instantiated and called as such:
164+
* let obj = NumMatrix::new(matrix);
165+
* let ret_1: i32 = obj.sum_region(row1, col1, row2, col2);
166+
*/
167+
168+
struct NumMatrix {
169+
// Of size (N + 1) * (M + 1)
170+
prefix_vec: Vec<Vec<i32>>,
171+
n: usize,
172+
m: usize,
173+
is_initialized: bool,
174+
ref_vec: Vec<Vec<i32>>,
175+
}
176+
177+
178+
/**
179+
* `&self` means the method takes an immutable reference.
180+
* If you need a mutable reference, change it to `&mut self` instead.
181+
*/
182+
impl NumMatrix {
183+
184+
fn new(matrix: Vec<Vec<i32>>) -> Self {
185+
NumMatrix {
186+
prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1],
187+
n: matrix.len(),
188+
m: matrix[0].len(),
189+
is_initialized: false,
190+
ref_vec: matrix,
191+
}
192+
}
193+
194+
fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 {
195+
if !self.is_initialized {
196+
self.initialize_prefix_vec();
197+
}
198+
// Since i32 will let `rustc` complain, just make it happy
199+
let row1: usize = row1 as usize;
200+
let col1: usize = col1 as usize;
201+
let row2: usize = row2 as usize;
202+
let col2: usize = col2 as usize;
203+
// Return the value in O(1)
204+
self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1]
205+
- self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1]
206+
}
207+
208+
fn initialize_prefix_vec(&mut self) {
209+
// Initialize the prefix sum vector
210+
for i in 0..self.n {
211+
for j in 0..self.m {
212+
self.prefix_vec[i + 1][j + 1] =
213+
self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j];
214+
}
215+
}
216+
self.is_initialized = true;
217+
}
218+
}
219+
```
220+
143221
### **Go**
144222

145223
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Your NumMatrix object will be instantiated and called as such:
3+
* let obj = NumMatrix::new(matrix);
4+
* let ret_1: i32 = obj.sum_region(row1, col1, row2, col2);
5+
*/
6+
7+
struct NumMatrix {
8+
// Of size (N + 1) * (M + 1)
9+
prefix_vec: Vec<Vec<i32>>,
10+
n: usize,
11+
m: usize,
12+
is_initialized: bool,
13+
ref_vec: Vec<Vec<i32>>,
14+
}
15+
16+
17+
/**
18+
* `&self` means the method takes an immutable reference.
19+
* If you need a mutable reference, change it to `&mut self` instead.
20+
*/
21+
impl NumMatrix {
22+
23+
fn new(matrix: Vec<Vec<i32>>) -> Self {
24+
NumMatrix {
25+
prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1],
26+
n: matrix.len(),
27+
m: matrix[0].len(),
28+
is_initialized: false,
29+
ref_vec: matrix,
30+
}
31+
}
32+
33+
fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 {
34+
if !self.is_initialized {
35+
self.initialize_prefix_vec();
36+
}
37+
// Since i32 will let `rustc` complain, just make it happy
38+
let row1: usize = row1 as usize;
39+
let col1: usize = col1 as usize;
40+
let row2: usize = row2 as usize;
41+
let col2: usize = col2 as usize;
42+
// Return the value in O(1)
43+
self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1]
44+
- self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1]
45+
}
46+
47+
fn initialize_prefix_vec(&mut self) {
48+
// Initialize the prefix sum vector
49+
for i in 0..self.n {
50+
for j in 0..self.m {
51+
self.prefix_vec[i + 1][j + 1] =
52+
self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j];
53+
}
54+
}
55+
self.is_initialized = true;
56+
}
57+
}

0 commit comments

Comments
 (0)