Design a 3D Binary Matrix with Efficient Layer Tracking - Problem

You need to design and implement a 3D Binary Matrix data structure that efficiently tracks which layer (2D slice) contains the most 1's.

Imagine you're managing a multi-story building where each floor is represented as a 2D grid, and you need to quickly determine which floor has the most occupied rooms (1's) at any given time.

Your Matrix3D class should support:

  • Matrix3D(int n) - Initialize an nร—nร—n matrix with all cells set to 0
  • setCell(int x, int y, int z) - Set matrix[x][y][z] to 1
  • unsetCell(int x, int y, int z) - Set matrix[x][y][z] to 0
  • largestMatrix() - Return the layer index x with the most 1's (if tied, return the largest x)

Example:

Matrix3D matrix = new Matrix3D(3);
matrix.setCell(0, 1, 1);     // Layer 0 has 1 one
matrix.setCell(1, 0, 0);     // Layer 1 has 1 one  
matrix.setCell(1, 2, 2);     // Layer 1 has 2 ones
matrix.largestMatrix();      // Returns 1 (layer 1 has most 1's)

Input & Output

example_1.py โ€” Basic Operations
$ Input: Matrix3D matrix = new Matrix3D(3); matrix.setCell(0, 1, 1); matrix.setCell(1, 0, 0); matrix.setCell(1, 2, 2); matrix.largestMatrix();
โ€บ Output: 1
๐Ÿ’ก Note: Layer 0 has 1 one at position [0,1,1]. Layer 1 has 2 ones at positions [1,0,0] and [1,2,2]. Layer 2 has 0 ones. Layer 1 has the most ones, so we return 1.
example_2.py โ€” Tie Handling
$ Input: Matrix3D matrix = new Matrix3D(2); matrix.setCell(0, 0, 0); matrix.setCell(1, 1, 1); matrix.largestMatrix();
โ€บ Output: 1
๐Ÿ’ก Note: Both layer 0 and layer 1 have exactly 1 one each. Since we need to return the largest index when there's a tie, we return 1.
example_3.py โ€” Unset Operations
$ Input: Matrix3D matrix = new Matrix3D(2); matrix.setCell(0, 0, 0); matrix.setCell(0, 1, 1); matrix.setCell(1, 0, 0); matrix.largestMatrix(); // returns 0 (layer 0 has 2, layer 1 has 1) matrix.unsetCell(0, 1, 1); matrix.largestMatrix(); // returns 1 (both have 1, prefer larger index)
โ€บ Output: 0, then 1
๐Ÿ’ก Note: Initially layer 0 has 2 ones and layer 1 has 1 one, so we return 0. After unsetting one cell in layer 0, both layers have 1 one each, so we return the larger index 1.

Visualization

Tap to expand
3D Matrix BuildingFloor 2 (Layer 2)Floor 1 (Layer 1)Floor 0 (Layer 0)Floor Counter SystemFloor0Count3Floor1Count5MAXFloor2Count2Operation Timeline1. setCell(1, 2, 1)Counter[1]: 4 โ†’ 5, Update MAX2. largestMatrix()Return maxLayer = 1 (O(1) time!)3. unsetCell(1, 0, 0)Counter[1]: 5 โ†’ 4, Still MAX4. largestMatrix()Return maxLayer = 1 (Still O(1)!)Key InsightMaintain counters + track maximumMost queries are O(1)!
Understanding the Visualization
1
Initialize Building
Create a 3ร—3ร—3 building with floor counters [0,0,0]
2
Car Parks (setCell)
When a car parks on floor 1, increment counter[1] and check if it's the new busiest floor
3
Car Leaves (unsetCell)
When a car leaves, decrement the counter and recheck maximum if needed
4
Query Busiest Floor
Return the tracked maximum floor instantly without counting all spaces
Key Takeaway
๐ŸŽฏ Key Insight: Instead of recounting all cells every time, maintain running counters for each layer and track the current maximum. This transforms an O(nยณ) query into an O(1) operation in most cases!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1) avg for all operations, O(n) worst case for unset

Set operations are O(1), unset is O(1) average but O(n) when we need to find new maximum

n
2n
โœ“ Linear Growth
Space Complexity
O(nยณ + n)

Need O(nยณ) for matrix plus O(n) for layer counters and tracking variables

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 0 โ‰ค x, y, z < n
  • At most 104 calls will be made to setCell, unsetCell, and largestMatrix
  • All coordinates (x, y, z) are guaranteed to be valid
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 23
23.5K Views
Medium-High 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