Maximum Number of Ones - Problem
Maximum Number of Ones is a fascinating optimization problem that challenges you to think about overlapping constraints.

You need to construct a binary matrix (containing only 0s and 1s) with dimensions width Γ— height. However, there's a crucial constraint: every square sub-matrix of size sideLength Γ— sideLength can contain at most maxOnes ones.

Your goal is to determine the maximum possible number of ones that can be placed in the entire matrix while respecting this constraint.

🎯 Key Insight: This isn't just about placing ones randomly - you need to understand how different positions in the matrix contribute to multiple overlapping sub-matrices, and optimize accordingly!

Input & Output

example_1.py β€” Basic Case
$ Input: width = 3, height = 3, sideLength = 2, maxOnes = 1
β€Ί Output: 4
πŸ’‘ Note: We can place ones at positions forming a checkerboard pattern within the constraints. Each 2Γ—2 sub-matrix contains exactly 1 one, and we maximize the total count.
example_2.py β€” Larger Matrix
$ Input: width = 2, height = 2, sideLength = 2, maxOnes = 2
β€Ί Output: 2
πŸ’‘ Note: The entire matrix is one 2Γ—2 sub-matrix, so we can place at most 2 ones anywhere in the matrix.
example_3.py β€” Edge Case
$ Input: width = 1, height = 1, sideLength = 1, maxOnes = 1
β€Ί Output: 1
πŸ’‘ Note: Single cell matrix with single cell sub-matrices. We can place 1 one in the single available position.

Constraints

  • 1 ≀ width ≀ 103
  • 1 ≀ height ≀ 103
  • 1 ≀ sideLength ≀ min(width, height)
  • 1 ≀ maxOnes ≀ sideLength2

Visualization

Tap to expand
🎯 Maximum Number of Ones - Position Equivalence6Γ—4 Matrix with sideLength=3(0,0)(0,1)(0,2)(0,0)(0,1)(0,2)(1,0)(1,1)(1,2)(1,0)(1,1)(1,2)(2,0)(2,1)(2,2)(2,0)(2,1)(2,2)(0,0)(0,1)(0,2)(0,0)(0,1)(0,2)Frequency AnalysisEquivalence Class Frequencies:●(0,0): 4 positions●(0,1): 4 positions●(0,2): 4 positions●(1,0): 2 positions●(1,1): 2 positions●(1,2): 2 positions... and 3 more classesGreedy Selection (maxOnes = 3)Step 1:Sort frequencies: [4, 4, 4, 2, 2, 2, 2, 2, 2]Step 2:Select top 3 classes: frequencies[0] + frequencies[1] + frequencies[2]Step 3:Result: 4 + 4 + 4 = 12 maximum onesπŸ’‘ Key:Positions with same modular coordinates have identical constraint patterns!⚑ Optimal O(sideLengthΒ²) Time Complexity
Understanding the Visualization
1
Identify Patterns
Every position (i,j) belongs to equivalence class (i%sideLength, j%sideLength)
2
Count Frequencies
Count how many matrix positions belong to each equivalence class
3
Greedy Selection
Sort classes by frequency and select the top maxOnes classes
4
Calculate Result
Sum the frequencies of selected classes
Key Takeaway
🎯 Key Insight: By recognizing that positions with the same modular coordinates (i%sideLength, j%sideLength) participate in constraints identically, we can group them into equivalence classes and apply a greedy strategy to maximize the total number of ones.
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen