Your algorithm sounds good, with O(n * n * k * log k)
time complexity and O(k * k)
memory. If you know that the values ββare integers in your example, you can get rid of log k
by replacing the map with an array. Otherwise, it is possible that your code implements inefficiency that implements the algorithm. Try choosing a time when you change n
and k
to see if the time is growing as expected.
As another possible direction, you can try a dynamic programming solution. Define the function f(x, y, a, b)
to compute a set of unique values ββ(possibly a bitmap) in the a
x b
rectangle anchored in (x, y)
. Then the problem is to find the maximum |f(x, y, k, k)|
. f(x, y, a, b)
calculated as the union of 4 or more smaller rectangular sets with a size of approximately a/2
x b/2
. If smaller rectangular sets are cached, you do not have to re-arrange them. It will take a lot of memory for the cache, but you can limit it by organizing your decompositions to use the power of 2 sizes. For instance,
f(x, y, 21, 21) = f(x, y, 16, 16) union f(x + 16, y, 4, 16) union f(x + 20, y, 1, 16) union f(x, y + 16, 16, 4) union f(x, y + 20, 16, 1) union f(x + 16, y + 16, 4, 4) union f(x + 20, y + 16, 1, 4) union f(x + 16, y + 20, 4, 1) union f(x + 20, y + 20, 1, 1)
I think this approach is more like O (n * n * log k * log k), and therefore it will pay off only for large k
values, such as more than 1000.