Strange Printer II - Problem
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.
Input & Output
Example 1 — Simple Valid Case
$
Input:
targetGrid = [[1,1,1],[3,1,3]]
›
Output:
true
💡 Note:
First print color 3 in rectangle covering entire grid, then print color 1 in rectangle covering positions (0,0) to (1,1). The overlapping region shows color 1 in final grid, so color 3 must be printed first.
Example 2 — Invalid Case
$
Input:
targetGrid = [[1,2],[2,1]]
›
Output:
false
💡 Note:
Color 1's bounding rectangle covers entire grid, as does color 2's. But both colors appear in each other's regions, creating a circular dependency that makes printing impossible.
Example 3 — Non-overlapping Colors
$
Input:
targetGrid = [[1,1],[2,2]]
›
Output:
true
💡 Note:
Colors 1 and 2 have non-overlapping bounding rectangles, so they can be printed in any order without conflicts.
Constraints
- m == targetGrid.length
- n == targetGrid[i].length
- 1 ≤ m, n ≤ 60
- 1 ≤ targetGrid[row][col] ≤ 60
Visualization
Tap to expand
Understanding the Visualization
1
Input
Target grid with colored cells that must be achieved
2
Analysis
Find color dependencies based on overlapping rectangular regions
3
Output
Determine if valid printing order exists (no circular dependencies)
Key Takeaway
🎯 Key Insight: Model as dependency graph - cycles mean impossible printing order
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code