Well here is an interesting problem. Suppose I have a table on an sql db filled with x,y coordinates (positive quadrant) and each having a color value i.e. the schema looks like <x , y, color>. The task is to detect the largest possible square with the same color. Ive been trying to solve this problem for hours and cant seem to make a dent in it.
I’m not looking for a solution but rather hints.
Note that this all has to happen in SQL mainly using various joins, grouping and aggregate operations. Some sample code would be nice.
If you only request the corners be the same color, you could do
Of course, the limit 1 won`t have much effect on performance because it will have to generate all squares anyways.
You can (greatly) improve the speed by adding the (color,x,y) and (color,y,x) indexes.
The execution plan will most likely end up: