Google | Find a Valid Painting Order | On-Site
Anonymous User
520

You will be given a targetGrid of size N x M, Each cell has a color targetGrid[i][j]. Consider you have a grid of size N x M with all cells initially equal 0 and you have a the following function:

void paint(int x, int y, int a, int b, int col) {
	assert(x <= a && y <= b && col != 0);
	assert(x >= 0 && x < N);
	assert(y >= 0 && y < M);
	for (int i = x; i <= a; i++) {
		for (int j = y; j <= b; j++) {
			grid[i][j] = col;
		}
	}
}

Find the mininum number of calls to convert grid to targetGrid and find all the parameters to paint function.

Example 1:

Input: targetGrid = [[1,1,1],[1,2,2],[1,2,2]]
Output:
min calls = 2
parameters = [[0, 0, 2, 2, 1], [1, 1, 2, 2, 2]]

Example 2:

Input: targetGrid = [[7,7,7,6],[6,5,6,5],[6,6,6,5]]
Output:
min calls = 4
parameters = [[0, 0, 2, 3, 6], [0, 0, 0, 2, 7], [1, 1, 1, 1, 5], [1, 3, 2, 3, 5]]
Comments (5)