Maximum Number of Ones

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def maximumNumberOfOnes(self, width: int, height: int, side_length: int, max_ones: int) -> int:
        ones_count = []

        for row in range(side_length):
            for col in range(side_length):
                horizontal_tiles = 1 + (width - col - 1) // side_length
                vertical_tiles = 1 + (height - row - 1) // side_length
                number_of_ones = horizontal_tiles * vertical_tiles
                ones_count.append(number_of_ones)

        ones_count.sort(reverse=True)
        return sum(ones_count[:max_ones])