Talentd
FleetCode

1591. Strange Printer II

Hard61.4% acceptance1 company

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.length
  • n == targetGrid[i].length
  • 1 <= m, n <= 60
  • 1 <= targetGrid[row][col] <= 60

Asked at 1 company

Hints open one at a time

Hint 1
Try thinking in reverse. Given the grid, how can you tell if a colour was painted last?
22:16
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def isPrintable(self, targetGrid: List[List[int]]) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
Input
[[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
Input
[[1,2,1],[2,1,2],[1,2,1]]
Your code is saved on this device.Solution