Leetcode Problem 1183. Maximum Number of Ones

1183. Maximum Number of Ones

Leetcode Solutions

Optimally Fill the Remainder Grids

  1. Count the number of full squares as (height // sideLength) * (width // sideLength).
  2. Count the number of ones in all full squares as answer = maxOnes * (height // sideLength) * (width // sideLength).
  3. Count the number of ones in one 'remainder grid' as cnt1 = min((height % sideLength) * (width % sideLength), maxOnes).
  4. Count the number of ones in all 'remainder grids' as cnt1 * ((height // sideLength) + (width // sideLength) + 1), and increment answer by this value.
  5. The maximum remaining ones for each incomplete square is maxOnes - cnt1.
  6. Find the longer side, calculate how many additional ones can be filled in each incomplete square, and then compute this value for the shorter side.
  7. Increment answer by these values.
  8. Return answer.
UML Thumbnail

Greedy Placement in Top-Left Window

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...