I’ve worked on ads at Meta, Snap, and Nextdoor. I also consult companies on building ads. If you are building your ad stack, feel free to reach out — let’s chat!
How many rooks can you place on a N x N chessboard such that no four of them form a rectangle? To be more precise, 100 <= N <= 1000 and we need to place 8 * N rooks on the board. Below is an example of rooks forming a rectangle.
Thought Process
Step 1: pen & paper
I couldn’t come up with anything better than 3 * N using the pattern below. Still far away from 8 * N!
Step 2: brute force
Then I decided to write some brute-force solution to get the most optimal answer for small values of N. I was hoping I can see a pattern there. This is what I got (for more than 6, it would take a very long time):
Code:
Step 3: greedy
Unfortunately doing greedy performs really poorly since it will fill up the first row and then you can’t place more than one rook on any other row, otherwise they’ll create a rectangle. However, it seems like if we can somehow fill some of the board more intelligently and then fill the rest using greedy, it would not be too bad.
Step 4: Divide & Conquer
What if we have a good solution for N, can we build a decent solution for 2*N? For example, given the solution for N = 5, we can build the solution below for N = 10.
This is actually great, because if we have a 8*N solution for some N, this method gives us the solution for N*2 x N*2 board! But can we do better? For example we can simply improve it by also filling one diagonal on one of the small squares:
However, it is not good enough to achieve 8*N for N = 100. Now, what if instead of filling the small diagonal, we let the greedy algorithm run on both small empty squares? However, filling greedy-ly using the code below, actually ends up filling the same diagonal, which is not great.
Step 5: Divide & Conquer + Guided Greedy
The trick to solve this problem was the realization that if you start filling squares where we have more degrees of freedom (sparse areas), you tend to achieve worse results compared to if you try to fit rooks in denser areas. So, I simply changed the order in which we check cells by looking at the main diagonal first, then the two smaller semi-diagonals next to it and slowly walking away in both directions. Something like this:
With this simple change, for N = 10, we get this board:
You may think just one more rook? But as N grows the difference becomes more significant. For example, for N = 50, we get this board:
Final Solution
Eventually, the divide & conquer + guided greedy did a pretty good job and for up to ~200, runs pretty quickly. Beyond that, we can just do the trick from “Step 4” and achieve a quick 8*N solution. Extra optimizations I did to make it work:
memoize the output for each N < 200
compress a pair of integers (each less than 1000) into one integer and use a Set<Integer> to avoid creating pairs.
Here is my code that passed all the tests. The answer for N = 200 looks like this:
Learning
Greedy solutions are often not ideal since they fall into a very simple local minima. However, combining with divide and conquer can sometimes avoid falling into those local minimas. The key point here is the hierarchical structure in which the greedy approach is applied. Also, in certain cases like this case, it makes more sense to fill greedily from denser areas first.