Strange Printer II - Problem
Imagine you have a magical printer that creates colorful rectangular patterns on a grid, but with some unusual restrictions! 🖨️

This strange printer has two special rules:
1. Rectangle Only: Each print operation creates a solid rectangular pattern of a single color, completely covering any existing colors in that area
2. One-Time Use: Once a color has been used for printing, it can never be used again

Given a target grid with various colors, your challenge is to determine: Is it possible to recreate this exact pattern using our strange printer?

You need to figure out if there exists a sequence of rectangular print operations that can produce the given targetGrid, where each color is used at most once.

Input & Output

example_1.py — Basic Valid Case
$ Input: targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
Output: true
💡 Note: We can print color 1 first (covering the entire 4×4 area), then print color 2 in the inner 2×2 rectangle. This creates the exact target pattern.
example_2.py — Impossible Case
$ Input: targetGrid = [[1,1,1],[3,1,3]]
Output: false
💡 Note: Color 3 appears at positions (1,0) and (1,2), forming an L-shape. Since the printer can only print rectangles, it's impossible to print color 3 without affecting other cells that should remain color 1.
example_3.py — Complex Valid Case
$ Input: targetGrid = [[1,2,2],[2,3,3],[3,3,1]]
Output: true
💡 Note: Valid printing order exists: First print 1 (entire grid), then 2 (top-left 2×2), finally 3 (bottom-right 2×2). Each step creates proper rectangular coverage.

Constraints

  • m == targetGrid.length
  • n == targetGrid[i].length
  • 1 ≤ m, n ≤ 60
  • 1 ≤ targetGrid[row][col] ≤ 60
  • Each color value represents a distinct paint color

Visualization

Tap to expand
🎨 Art Studio: Strange Printer VisualizationStep 1: Artists and Their RectanglesArtist 1Artist 2Artist 3Step 2: Target ArtworkWhat we need to createStep 3: Dependency Analysis132must paint beforemust paint beforeStep 4: Painting Sequence1. Artist 1 paints entire canvas2. Artist 3 paints bottom-right3. Artist 2 paints top-rightStep 5: Cycle Detection Result✅ NO CYCLES DETECTEDValid painting order exists: 1 → 3 → 2The target artwork CAN be created!💡 Key InsightIf color A is covered by color B somewhere,then A must have been painted before B.Cycles in dependencies = impossible!
Understanding the Visualization
1
🎯 Analyze Target
Look at the final artwork and identify where each color appears. Find the smallest rectangle that contains all instances of each color.
2
🔍 Find Dependencies
If artist A's rectangle overlaps with areas where color B is visible in the final artwork, then A must have painted before B (since B painted over A in those areas).
3
📊 Build Graph
Create a dependency graph where each color points to colors that must be painted after it. This captures the 'painting order' constraints.
4
🔄 Check Cycles
Use DFS to detect cycles in the dependency graph. A cycle means contradictory requirements (A before B, B before C, C before A), making the artwork impossible to create.
5
✅ Determine Result
No cycles = artwork is possible (any topological ordering works). Cycles detected = impossible to create the target artwork.
Key Takeaway
🎯 Key Insight: Transform the printing problem into a graph problem. Dependencies between colors create a directed graph, and the existence of a solution depends entirely on whether this dependency graph is acyclic. Topological sorting provides an elegant O(k² × m × n) solution.
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
18.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen