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 0setCell(int x, int y, int z)- Set matrix[x][y][z] to 1unsetCell(int x, int y, int z)- Set matrix[x][y][z] to 0largestMatrix()- Return the layer indexxwith 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
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
โ Linear Growth
Space Complexity
O(nยณ + n)
Need O(nยณ) for matrix plus O(n) for layer counters and tracking variables
โ Quadratic Space
Constraints
- 1 โค n โค 100
- 0 โค x, y, z < n
-
At most 104 calls will be made to
setCell,unsetCell, andlargestMatrix - All coordinates (x, y, z) are guaranteed to be valid
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code