Skip to content

Commit 4f080fd

Browse files
committed
1 parent 297f988 commit 4f080fd

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1199,3 +1199,4 @@ mod s1585_check_if_string_is_transformable_with_substring_sort_operations;
11991199
mod s1588_sum_of_all_odd_length_subarrays;
12001200
mod s1589_maximum_sum_obtained_of_any_permutation;
12011201
mod s1590_make_sum_divisible_by_p;
1202+
mod s1591_strange_printer_ii;
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* [1591] Strange Printer II
3+
*
4+
* There is a strange printer with the following two special requirements:
5+
*
6+
* On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.
7+
* Once the printer has used a color for the above operation, the same color cannot be used again.
8+
*
9+
* You are given a m x n matrix targetGrid, where targetGrid[row][col] is the color in the position (row, col) of the grid.
10+
* Return true if it is possible to print the matrix targetGrid, otherwise, return false.
11+
*
12+
* Example 1:
13+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print1.jpg" style="width: 600px; height: 175px;" />
14+
* Input: targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
15+
* Output: true
16+
*
17+
* Example 2:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print2.jpg" style="width: 600px; height: 367px;" />
19+
* Input: targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
20+
* Output: true
21+
*
22+
* Example 3:
23+
*
24+
* Input: targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
25+
* Output: false
26+
* Explanation: It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* m == targetGrid.length
32+
* n == targetGrid[i].length
33+
* 1 <= m, n <= 60
34+
* 1 <= targetGrid[row][col] <= 60
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/strange-printer-ii/
40+
// discuss: https://leetcode.com/problems/strange-printer-ii/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
// Credit: https://leetcode.com/problems/strange-printer-ii/solutions/3175080/just-a-runnable-solution/
46+
pub fn is_printable(target_grid: Vec<Vec<i32>>) -> bool {
47+
let (m, n) = (target_grid.len() as i32, target_grid[0].len() as i32);
48+
let mut dep = vec![std::collections::HashSet::new(); 61];
49+
for i in 1..=60 {
50+
let (mut minx, mut miny, mut maxx, mut maxy) = (m, n, -1, -1);
51+
for x in 0..m {
52+
for y in 0..n {
53+
if target_grid[x as usize][y as usize] == i {
54+
minx = minx.min(x);
55+
miny = miny.min(y);
56+
maxx = maxx.max(x);
57+
maxy = maxy.max(y);
58+
}
59+
}
60+
}
61+
for tx in minx..=maxx {
62+
for ty in miny..=maxy {
63+
if target_grid[tx as usize][ty as usize] != i {
64+
dep[i as usize].insert(target_grid[tx as usize][ty as usize]);
65+
}
66+
}
67+
}
68+
}
69+
let mut vis = vec![0; 61];
70+
for i in 1..=60 {
71+
if vis[i] == 0 && Self::has_circle(i, &dep, &mut vis) {
72+
return false;
73+
}
74+
}
75+
true
76+
}
77+
78+
fn has_circle(
79+
curr: usize,
80+
dep: &Vec<std::collections::HashSet<i32>>,
81+
vis: &mut Vec<i32>,
82+
) -> bool {
83+
vis[curr] = 1;
84+
for &d in dep[curr].iter() {
85+
let d = d as usize;
86+
if vis[d] == 2 {
87+
continue;
88+
}
89+
if vis[d] == 1 {
90+
return true;
91+
}
92+
if Self::has_circle(d, dep, vis) {
93+
return true;
94+
}
95+
}
96+
vis[curr] = 2;
97+
false
98+
}
99+
}
100+
101+
// submission codes end
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use super::*;
106+
107+
#[test]
108+
fn test_1591_example_1() {
109+
let target_grid = vec![
110+
vec![1, 1, 1, 1],
111+
vec![1, 2, 2, 1],
112+
vec![1, 2, 2, 1],
113+
vec![1, 1, 1, 1],
114+
];
115+
116+
let result = true;
117+
118+
assert_eq!(Solution::is_printable(target_grid), result);
119+
}
120+
121+
#[test]
122+
fn test_1591_example_2() {
123+
let target_grid = vec![
124+
vec![1, 1, 1, 1],
125+
vec![1, 1, 3, 3],
126+
vec![1, 1, 3, 4],
127+
vec![5, 5, 1, 4],
128+
];
129+
130+
let result = true;
131+
132+
assert_eq!(Solution::is_printable(target_grid), result);
133+
}
134+
135+
#[test]
136+
fn test_1591_example_3() {
137+
let target_grid = vec![vec![1, 2, 1], vec![2, 1, 2], vec![1, 2, 1]];
138+
139+
let result = false;
140+
141+
assert_eq!(Solution::is_printable(target_grid), result);
142+
}
143+
}

0 commit comments

Comments
 (0)