human
I found an interesting problem on https://leetcode.com/problems/strange-printer-ii/:
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.
Example 4:
Input: targetGrid = [[1,1,1],[3,1,3]]
Output: false

 
Constraints:

m == targetGrid.length
n == targetGrid[i].length
1 <= m, n <= 60
1 <= targetGrid[row][col] <= 60

I tried it in Python, but could not do it. Can you solve it?

 What is a Python program to print a solid rectangular pattern of a single color on the grid?
assistant
#Here is Python program to print a solid rectangular pattern of a single color on the grid.

from collections import defaultdict
class Solution:
    def all_same(self, left, right, bottom, top, color):
        for i in range(bottom, top+1):
            for j in range(left, right+1):
                if self.grid[i][j] and self.grid[i][j] != color:
                    return False
        return True
    
    def fill(self, left, right, bottom, top):
        for i in range(bottom, top+1):
            for j in range(left, right+1):
                self.grid[i][j] = 0
                
    def isPrintable(self, targetGrid: List[List[int]]) -> bool:
        self.rect = defaultdict(lambda : [10**9, -10**9, 10**9, -10**9])
        for i, row in enumerate(targetGrid):
            for j, color in enumerate(row):
                self.rect[color][0] = min(self.rect[color][0], j)
                self.rect[color][1] = max(self.rect[color][1], j)
                self.rect[color][2] = min(self.rect[color][2], i)
                self.rect[color][3] = max(self.rect[color][3], i)
        self.grid = targetGrid
        while self.rect:
            for color in self.rect:
                left, right, bottom, top = self.rect[color]
                if self.all_same(left, right, bottom, top, color):
                    self.fill(left, right, bottom, top)
                    self.rect.pop(color)
                    break
            else:         
                return False
        return True