Strange Printer II Problem
Description
LeetCode Problem 1591.
There is a strange printer with the following two special requirements:
- 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.
- Once the printer has used a color for the above operation, the same color cannot be used again.
You are given a m x n matrix targetGrid, where targetGrid[row][col] is the color in the position (row, col) of the grid.
Return true if it is possible to print the matrix targetGrid, otherwise, return false.
Example 1:

Input: targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
Output: true
Example 2:

Input: targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
Output: true
Example 3:
Input: targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
Output: false
Explanation: It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
Constraints:
m == targetGrid.lengthn == targetGrid[i].length1 <= m, n <= 601 <= targetGrid[row][col] <= 60
Solution
Approach: Bounding Boxes + Topological Sort
Each color is printed once as a rectangle, and cells it shows at the end were never covered later. So the rectangle of color c must contain all cells of color c, and the smallest such choice is its bounding box (min/max row and column of its cells). Any choice larger than the bounding box only adds constraints, so we can assume the bounding box.
Inside that box, every cell that shows a different color d was painted over later, which means c must be printed before d. Add an edge c -> d for each such pair.
The grid can be printed exactly when these "before" constraints can all hold at once, i.e. when the graph has no cycle. Check that with Kahn's algorithm: repeatedly take colors with no remaining incoming edges; if all colors are taken, the order exists. Example 3 fails because the box of 1 contains 2 and the box of 2 contains 1, a cycle.
Complexity: O(C · mn + C2) time, where C ≤ 60 is the number of colors, and O(C2) extra space for the edges.
class Solution:
def isPrintable(self, targetGrid: List[List[int]]) -> bool:
C = 61
# bounding box of each color: the rectangle it must have been printed as
top, left, bottom, right = [60] * C, [60] * C, [-1] * C, [-1] * C
for i, row in enumerate(targetGrid):
for j, c in enumerate(row):
top[c] = min(top[c], i)
bottom[c] = max(bottom[c], i)
left[c] = min(left[c], j)
right[c] = max(right[c], j)
# edge c -> d: d covers part of c's rectangle, so c was printed before d
after = [set() for _ in range(C)]
for c in range(1, C):
for i in range(top[c], bottom[c] + 1):
for j in range(left[c], right[c] + 1):
d = targetGrid[i][j]
if d != c:
after[c].add(d)
indeg = [0] * C
for c in range(C):
for d in after[c]:
indeg[d] += 1
# Kahn's algorithm: printable iff the order constraints have no cycle
queue = [c for c in range(C) if indeg[c] == 0]
done = 0
while queue:
c = queue.pop()
done += 1
for d in after[c]:
indeg[d] -= 1
if indeg[d] == 0:
queue.append(d)
return done == C