|
50 | 50 |
|
51 | 51 | ## Solutions
|
52 | 52 |
|
| 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 | + |
53 | 71 | <!-- tabs:start -->
|
54 | 72 |
|
55 | 73 | ### **Python3**
|
@@ -163,6 +181,72 @@ public:
|
163 | 181 | };
|
164 | 182 | ```
|
165 | 183 |
|
| 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 | + |
166 | 250 | ### **Go**
|
167 | 251 |
|
168 | 252 | ```go
|
|
0 commit comments