Given four integers width
, height
, sideLength
and maxOnes
, you are asked to place some 1
s into a width x height
grid and obtain the maximum number of ones on it.
You are allowed to place a 1
into any cell (i, j)
if and only if i % sideLength == j % sideLength
.
Return the maximum number of ones you can put on the grid.
Example 1:
Input: width = 3, height = 3, sideLength = 2, maxOnes = 1 Output: 4 Explanation: In this example, the cells where you can place a 1 are shown in light blue. The number of ones in each 2x2 square is at most 1. In the 3x3 board, there are four 2x2 subgrids. Placing a 1 on the dark blue cells is the optimal way to reach the maximum number of ones on the board.
Example 2:
Input: width = 2, height = 2, sideLength = 2, maxOnes = 2 Output: 4 Explanation: There is only one 2x2 grid in this case, and you can place 1s on all the cells.
Constraints:
1 <= width, height <= 105
1 <= sideLength <= min(width, height)
1 <= maxOnes <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a grid filled with zeros and ones. Our goal is to find a smaller rectangle that contains the most ones. The brute force method is like checking every possible rectangle size and location to see which one has the most ones.
Here's how the algorithm would work step-by-step:
def find_maximum_ones_brute_force(grid):
rows = len(grid)
cols = len(grid[0])
maximum_ones_found = 0
for top_row in range(rows):
for left_col in range(cols):
# Iterate through all possible top-left corners.
for height in range(1, rows - top_row + 1):
for width in range(1, cols - left_col + 1):
# Iterate through all possible rectangle sizes.
current_ones = 0
for row in range(top_row, top_row + height):
for col in range(left_col, left_col + width):
current_ones += grid[row][col]
# Track the rectangle with max ones.
if current_ones > maximum_ones_found:
maximum_ones_found = current_ones
return maximum_ones_found
The goal is to place rectangles of a specific size within a larger grid to maximize the number of smaller squares that can fit. The best approach focuses on efficiently placing the rectangles and understanding how they interact with each other without trying every possible position. This allows us to arrive at the optimal answer by using math and logic instead of brute force.
Here's how the algorithm would work step-by-step:
def maximum_number_of_ones(grid_row_size, grid_column_size, rectangle_row_size, rectangle_column_size, max_rectangles):
row_fit = grid_row_size // rectangle_row_size
column_fit = grid_column_size // rectangle_column_size
max_fit = row_fit * column_fit
actual_rectangles = min(max_rectangles, max_fit)
#We choose to use row_fit * column_fit rectangles
total_ones = actual_rectangles * rectangle_row_size * rectangle_column_size
row_remainder = grid_row_size % rectangle_row_size
column_remainder = grid_column_size % rectangle_column_size
# We want to determine the optimal offset
row_offset = (grid_row_size + rectangle_row_size - 1) // rectangle_row_size
column_offset = (grid_column_size + rectangle_column_size - 1) // rectangle_column_size
num_rows = grid_row_size // rectangle_row_size
num_cols = grid_column_size // rectangle_column_size
remaining_rectangles = min(max_rectangles, row_offset * column_offset)
#Greedily put as many rectangles as possible.
if row_remainder != 0 and column_remainder != 0:
remaining_rectangles = min(max_rectangles, num_rows * column_offset + row_offset * num_cols + row_offset * column_offset - num_rows * num_cols)
number_of_rectangles_placed = min(max_rectangles,
(grid_row_size // rectangle_row_size) * (grid_column_size // rectangle_column_size) +
max(0, (grid_row_size % rectangle_row_size) * (grid_column_size // rectangle_column_size) // rectangle_column_size) +
max(0, (grid_column_size % rectangle_column_size) * (grid_row_size // rectangle_row_size) // rectangle_row_size) +
max(0, (grid_row_size % rectangle_row_size) * (grid_column_size % rectangle_column_size) // (rectangle_row_size * rectangle_column_size)))
total_ones = number_of_rectangles_placed * rectangle_row_size * rectangle_column_size
return number_of_rectangles_placed * rectangle_row_size * rectangle_column_size
Case | How to Handle |
---|---|
width, height, or sideLength is zero | Return 0 immediately as no grid or subgrids can exist, preventing division by zero errors. |
maxOnes is zero | Return 0 immediately since no ones can be placed. |
width and height are smaller than sideLength | Effectively only one subgrid exists, so place min(width * height, maxOnes) ones. |
width * height is smaller than maxOnes | Return width * height, because we cannot place more ones than there are cells in the grid. |
width or height is a very large number (potential integer overflow in calculations) | Use long integers for intermediate calculations to prevent integer overflow when calculating areas or counts. |
sideLength is 1 | Place 'maxOnes' number of 1s but no more than width * height in total, i.e. return min(width * height, maxOnes). |
maxOnes is greater than the number of subgrids | The final solution accounts for this by limiting the number of ones placed to the available grid cells. |
width, height, and sideLength are all 1 | Return min(1, maxOnes), since we have a 1x1 grid. |