fill a defined space with planes or cubes?

I need to create a script that will fill an area with as few squares or cubes as possible. it needs to fill this space completely. all the edges are either horizontal or vertical. The lines that define the space never intersect. all the edges are on a grid with 0.5 spacing. if it is possible, filling the space with one mesh that fits it exactly would be the least taxing solution, but I have no idea how I would do that. knowing the total area of the space would be awesome too.

I know how I could fill the space with 0.5x0.5 cubes, but that takes a lot of computer power. I need a low cpu solution.

here is an example of the kind of area I need to fill (blue):


I had the same idea, but for some shapes it won’t work. it would get stuck and end up leaving some areas unfinished.

If you show me what you have so far, I’ll take a look at it.