I'm writing a tool for working with tiled images. One feature is to convert a whole image into a tileset and tilemap, e.g. a 160x144px image would have a set of unique 8x8 tiles and a 20x18 map of tile IDs.
The next goal is to support palettes. On some of the older platforms that used tiled graphics, you might have 8 palettes of 4 colors each, or 16 of 16 each. I want to automatically create a set of palettes that fits within the N-by-K limit, using as few palettes as possible; and assign those palettes to the tilemap, or alert if it's not possible.
There are some obvious first steps: if any single tile uses more than K colors, it won't be possible; and once that's been checked, any tile whose colors are a subset of another can trivially share its palette. The difficulty is handling partially overlapping palettes. Consider 17 tiles, each with 15 unique colors; if there's enough overlap, they can fit within 16x16 color palettes, but it might not be possible.
I expect a dynamic programming solution would work here. At any stage in the problem, one has a partial assignment of tiles to palettes; and the decision is to which of the N palettes to assign the next tile. (The tile might not even have any colors in common with the optimal choice of palette at that time; consider 4 tiles each with 4 unique colors, they could all use a single 16-color palette.)
Has this particular problem been solved already? Is there a known algorithm for it, or just the general tips of all dynamic programming?