#1591

Strange Printer II

expert · 1165 · lc hard +32 · verified · 60.7% accepted · 691 likes · top 60%

Description

A printer fills solid rectangles with a single color each turn, overwriting existing colors. Each color may only be used once. Given a target grid targetGrid, return true if some valid sequence of print operations can produce it, 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.

Code

1
2
3