I can’t figure out where to apply dynamic programming here.
can’t I just place the erase at every possible place and calculate the number of white lines, and the max of all will be the answer, it is the brute force and will take time. How to approach it optimally?
Square Painting : : Dynamic programming
Hey @utkarsh.lal9430310535
Let’s solve the problem for rectangle W×H (W≥H). Of course, we can cover all rectangle with itself for cost W. To get something smaller than W we have to leave at least one column uncovered — otherwise we pay at least sum of w over all rectangles which is at least W. This gives us an idea to use DP on rectangles to solve the problem: dp[x1][x2][y1][y2] is minimal cost to cover the rectangle [x1;x2)×[y1;y2). It is initialized by max(x2−x1,y2−y1), and we have to try not to cover every column/row. Of course, we have to check if it is all white from the beginning; to do that we will precalculate 2D prefix sums. Total complexity is O(5*n).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Sorry but i am not able to get this, the problem says that we could use the eraser only once so i was thinking to check for every valid position of placing the eraser,also i can’t understand the solution for your W x H sub problem(What is W and H and in this case what’s the size of eraser).
@utkarsh.lal9430310535
W and H are width and height of rectangle drawing canvas
For our question W = H = N because its a square
If you use eraser at each valid position complexity will be 0(n^4)
Your solution can have at max 0(n^3)
So pre-calculate number of black cells in each k size segment in rows and cols
When you place an erase somewhere, row will become white if black cells in row == black cells in k size segment from that position
Similarly you can solve for columns
So n^2 cells and n time at each cells gives 0(n^3)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
hey! can someone put up the solution for this