Strange Printer II — LeetCode 1591 Python Solution

HardGraphTopological SortArrayMatrix
Problem
#1591
Reading time
2 min

The 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.

Example

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

Complexity

MeasureComplexity
TimeO(V+E)
SpaceO(V) auxiliary

Pattern: Topological Sort

Order a set of tasks so that every dependency comes before the thing that needs it. LeetCode 1591. Strange Printer II is filed here because LeetCode tags it Topological Sort, which is the vocabulary this hub collects.

The topological sort guide has the Python template for the pattern and the 32 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1591. Strange Printer II?
LeetCode 1591. Strange Printer II is rated Hard on LeetCode.
What is the time complexity of LeetCode 1591. Strange Printer II?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 1591. Strange Printer II?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 1591. Strange Printer II cover?
LeetCode 1591. Strange Printer II is tagged Graph, Topological Sort, Array and Matrix on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview