Skip to content

Commit 4cb1ae8

Browse files
authoredJul 2, 2023
feat: add rust solution to lc problem: No.2132 (#1123)
1 parent 6e301a5 commit 4cb1ae8

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed
 

‎solution/2100-2199/2132.Stamping the Grid/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060

6161
**方法一:二维前缀和 + 二维差分**
6262

63+
根据题给的约束,很容易推出,一个格子能贴邮票的条件为,在加上邮票的长和宽后,右下角不越界的情况下,当前子区域中所有格子的和为 0。
64+
65+
那么显然我们可以维护一个二维的前缀和数组,在 `O(1)` 的时间复杂度下就可以判断每次遍历到的格子是否能贴邮票。
66+
67+
而因为贴邮票的操作,可以概括为将当前子区域的所有格子的值都置为 `1`,很自然的就能想到用一个二维的差分数组来维护贴邮票后的状态。
68+
69+
最后只要对该差分数组再求一次二维前缀和,只要当前格子的和为 `0`,就意味着存在没有覆盖完全的情况,直接返回 `false` 即可。
70+
71+
需要注意的是二维数组的下标关系,具体参考如下。
72+
6373
`s[i + 1][j + 1]` 表示第 i 行第 j 列左上部分所有元素之和,其中 i, j 下标从 0 开始。
6474

6575
`s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`
@@ -183,6 +193,72 @@ public:
183193
};
184194
```
185195
196+
### **Rust**
197+
198+
```rust
199+
impl Solution {
200+
pub fn possible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {
201+
let n: usize = grid.len();
202+
let m: usize = grid[0].len();
203+
204+
let mut prefix_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
205+
206+
// Initialize the prefix vector
207+
for i in 0..n {
208+
for j in 0..m {
209+
prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j];
210+
}
211+
}
212+
213+
let mut diff_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
214+
215+
// Construct the difference vector based on prefix sum vector
216+
for i in 0..n {
217+
for j in 0..m {
218+
// If the value of the cell is one, just bypass this
219+
if grid[i][j] == 1 {
220+
continue;
221+
}
222+
// Otherwise, try stick the stamp
223+
let x: usize = i + stamp_height as usize;
224+
let y: usize = j + stamp_width as usize;
225+
// Check the bound
226+
if x <= n && y <= m {
227+
// If the region can be sticked (All cells are empty, which means the sum will be zero)
228+
if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 {
229+
// Update the difference vector
230+
diff_vec[i][j] += 1;
231+
diff_vec[x][y] += 1;
232+
233+
diff_vec[x][j] -= 1;
234+
diff_vec[i][y] -= 1;
235+
}
236+
}
237+
}
238+
}
239+
240+
let mut check_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
241+
242+
// Check the prefix sum of difference vector, to determine if there is any empty cell left
243+
for i in 0..n {
244+
for j in 0..m {
245+
// If the value of the cell is one, just bypass this
246+
if grid[i][j] == 1 {
247+
continue;
248+
}
249+
// Otherwise, check if the region is empty, by calculating the prefix sum of difference vector
250+
check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j];
251+
if check_vec[i + 1][j + 1] == 0 {
252+
return false;
253+
}
254+
}
255+
}
256+
257+
true
258+
}
259+
}
260+
```
261+
186262
### **Go**
187263

188264
```go

‎solution/2100-2199/2132.Stamping the Grid/README_EN.md

+84
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@
5050

5151
## Solutions
5252

53+
Based on the constraints provided by the question, it's easy to infer that a cell can accommodate a stamp if, after adding the length and width of the stamp, the bottom-right corner does not exceed the boundary, and the sum of all cells in the current sub-area is zero.
54+
55+
Apparently, we can maintain a two-dimensional prefix sum array, and in `O(1)` time complexity, we can judge whether each cell traversed can accommodate a stamp.
56+
57+
Since the action of affixing a stamp can be generalized to setting the values of all cells in the current sub-area to `1`, it's natural to think of maintaining the state after stamp affixing using a two-dimensional difference array.
58+
59+
Finally, just calculate the two-dimensional prefix sum for this difference array again.
60+
61+
If the sum of the current cell is `0`, it means there are cases that have not been completely covered, and you can directly return `false`.
62+
63+
It's worth noting the subscript relationship of the two-dimensional array, which is as follows.
64+
65+
`s[i + 1][j + 1]` represents the sum of all elements in the upper left part of the i-th row and j-th column, where the subscript i, j starts from 0.
66+
67+
So `s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`.
68+
69+
For a sub-matrix with (x1, y1) as the upper left corner and (x2, y2) as the bottom right corner, the sum `sub = s[x2 + 1][y2 + 1] - s[x2 + 1][y1] - s[x1][y2 + 1] + s[x1][y1]`.
70+
5371
<!-- tabs:start -->
5472

5573
### **Python3**
@@ -163,6 +181,72 @@ public:
163181
};
164182
```
165183
184+
### **Rust**
185+
186+
```rust
187+
impl Solution {
188+
pub fn possible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {
189+
let n: usize = grid.len();
190+
let m: usize = grid[0].len();
191+
192+
let mut prefix_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
193+
194+
// Initialize the prefix vector
195+
for i in 0..n {
196+
for j in 0..m {
197+
prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j];
198+
}
199+
}
200+
201+
let mut diff_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
202+
203+
// Construct the difference vector based on prefix sum vector
204+
for i in 0..n {
205+
for j in 0..m {
206+
// If the value of the cell is one, just bypass this
207+
if grid[i][j] == 1 {
208+
continue;
209+
}
210+
// Otherwise, try stick the stamp
211+
let x: usize = i + stamp_height as usize;
212+
let y: usize = j + stamp_width as usize;
213+
// Check the bound
214+
if x <= n && y <= m {
215+
// If the region can be sticked (All cells are empty, which means the sum will be zero)
216+
if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 {
217+
// Update the difference vector
218+
diff_vec[i][j] += 1;
219+
diff_vec[x][y] += 1;
220+
221+
diff_vec[x][j] -= 1;
222+
diff_vec[i][y] -= 1;
223+
}
224+
}
225+
}
226+
}
227+
228+
let mut check_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
229+
230+
// Check the prefix sum of difference vector, to determine if there is any empty cell left
231+
for i in 0..n {
232+
for j in 0..m {
233+
// If the value of the cell is one, just bypass this
234+
if grid[i][j] == 1 {
235+
continue;
236+
}
237+
// Otherwise, check if the region is empty, by calculating the prefix sum of difference vector
238+
check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j];
239+
if check_vec[i + 1][j + 1] == 0 {
240+
return false;
241+
}
242+
}
243+
}
244+
245+
true
246+
}
247+
}
248+
```
249+
166250
### **Go**
167251

168252
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
impl Solution {
2+
pub fn possible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {
3+
let n: usize = grid.len();
4+
let m: usize = grid[0].len();
5+
6+
let mut prefix_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
7+
8+
// Initialize the prefix vector
9+
for i in 0..n {
10+
for j in 0..m {
11+
prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j];
12+
}
13+
}
14+
15+
let mut diff_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
16+
17+
// Construct the difference vector based on prefix sum vector
18+
for i in 0..n {
19+
for j in 0..m {
20+
// If the value of the cell is one, just bypass this
21+
if grid[i][j] == 1 {
22+
continue;
23+
}
24+
// Otherwise, try stick the stamp
25+
let x: usize = i + stamp_height as usize;
26+
let y: usize = j + stamp_width as usize;
27+
// Check the bound
28+
if x <= n && y <= m {
29+
// If the region can be sticked (All cells are empty, which means the sum will be zero)
30+
if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 {
31+
// Update the difference vector
32+
diff_vec[i][j] += 1;
33+
diff_vec[x][y] += 1;
34+
35+
diff_vec[x][j] -= 1;
36+
diff_vec[i][y] -= 1;
37+
}
38+
}
39+
}
40+
}
41+
42+
let mut check_vec: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
43+
44+
// Check the prefix sum of difference vector, to determine if there is any empty cell left
45+
for i in 0..n {
46+
for j in 0..m {
47+
// If the value of the cell is one, just bypass this
48+
if grid[i][j] == 1 {
49+
continue;
50+
}
51+
// Otherwise, check if the region is empty, by calculating the prefix sum of difference vector
52+
check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j];
53+
if check_vec[i + 1][j + 1] == 0 {
54+
return false;
55+
}
56+
}
57+
}
58+
59+
true
60+
}
61+
}

0 commit comments

Comments
 (0)