Skip to content

Commit

Permalink
add: 用邮票贴满网格图
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 14, 2023
1 parent 03a6872 commit 2488b59
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [用邮票贴满网格图](src/array/stamping_the_grid.rs) [贪心, 数组, 矩阵, 前缀和]

- LeetCode 2132. 用邮票贴满网格图 <https://leetcode.cn/problems/stamping-the-grid>

- [打家劫舍](src/array/house_robber.rs) [数组, 动态规划]

- LeetCode 198. 打家劫舍 <https://leetcode.cn/problems/house-robber>
Expand Down
Binary file added images/array/stamping_the_grid.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ pub mod equal_row_and_column_pairs;
pub mod number_of_unequal_triplets_in_array;
pub mod number_of_times_binary_string_is_prefix_aligned;
pub mod house_robber;
pub mod stamping_the_grid;
78 changes: 78 additions & 0 deletions src/array/stamping_the_grid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 用邮票贴满网格图
// https://leetcode.cn/problems/stamping-the-grid
// INLINE ../../images/array/stamping_the_grid.jpeg

pub struct Solution;

impl Solution {
pub fn possible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {
let m = grid.len();
let n = grid[0].len();
let stamp_height = stamp_height as usize;
let stamp_width = stamp_width as usize;

// 检查网格中是否有任何空白(0),如果没有,则直接返回 true
if grid.iter().all(|row| row.iter().all(|&cell| cell == 1)) {
return true;
}

// 如果邮票尺寸大于网格本身,则无法放置邮票
if stamp_height > m || stamp_width > n {
return false;
}

let mut prefix_sum = vec![vec![0; n + 1]; m + 1];
let mut diff = vec![vec![0; n + 1]; m + 1];

// 计算前缀和
for i in 1..=m {
for j in 1..=n {
prefix_sum[i][j] = grid[i - 1][j - 1] + prefix_sum[i - 1][j] + prefix_sum[i][j - 1]
- prefix_sum[i - 1][j - 1];
}
}

// 使用差分数组记录邮票放置情况
for i in 1..=m - stamp_height + 1 {
for j in 1..=n - stamp_width + 1 {
let x = i + stamp_height - 1;
let y = j + stamp_width - 1;
if prefix_sum[x][y] - prefix_sum[x][j - 1] - prefix_sum[i - 1][y]
+ prefix_sum[i - 1][j - 1]
== 0
{
diff[i][j] += 1;
if y + 1 <= n {
diff[i][y + 1] -= 1;
}
if x + 1 <= m {
diff[x + 1][j] -= 1;
}
if x + 1 <= m && y + 1 <= n {
diff[x + 1][y + 1] += 1;
}
}
}
}

// 更新差分数组并检查是否每个空白格都至少被一个邮票覆盖
for i in 1..=m {
for j in 1..=n {
if i > 1 {
diff[i][j] += diff[i - 1][j];
}
if j > 1 {
diff[i][j] += diff[i][j - 1];
}
if i > 1 && j > 1 {
diff[i][j] -= diff[i - 1][j - 1];
}
if diff[i][j] == 0 && grid[i - 1][j - 1] == 0 {
return false;
}
}
}

true
}
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ pub mod equal_row_and_column_pairs_test;
pub mod number_of_unequal_triplets_in_array_test;
pub mod number_of_times_binary_string_is_prefix_aligned_test;
pub mod house_robber_test;
pub mod stamping_the_grid_test;
39 changes: 39 additions & 0 deletions tests/array/stamping_the_grid_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use rust_practice::array::stamping_the_grid::Solution;

#[test]
fn possible_to_stamp() {
// 示例 1:
// 输入:grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 4, stampWidth = 3
// 输出:true
// 解释:我们放入两个有重叠部分的邮票(图中标号为 1 和 2),它们能覆盖所有与空格子。
let grid = vec![
vec![1, 0, 0, 0],
vec![1, 0, 0, 0],
vec![1, 0, 0, 0],
vec![1, 0, 0, 0],
vec![1, 0, 0, 0],
];
let stamp_height = 4;
let stamp_width = 3;
assert_eq!(
Solution::possible_to_stamp(grid, stamp_height, stamp_width),
true
);

// 示例 2:
// 输入:grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2
// 输出:false
// 解释:没办法放入邮票覆盖所有的空格子,且邮票不超出网格图以外。
let grid = vec![
vec![1, 0, 0, 0],
vec![0, 1, 0, 0],
vec![0, 0, 1, 0],
vec![0, 0, 0, 1],
];
let stamp_height = 2;
let stamp_width = 2;
assert_eq!(
Solution::possible_to_stamp(grid, stamp_height, stamp_width),
false
);
}

0 comments on commit 2488b59

Please sign in to comment.