Design a 3D Binary Matrix with Efficient Layer Tracking Problem
#3391
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3391.
You are given a n x n x n binary 3D array matrix.
Implement the Matrix3D class:
Matrix3D(int n)Initializes the object with the 3D binary arraymatrix, where all elements are initially set to 0.void setCell(int x, int y, int z)Sets the value atmatrix[x][y][z]to 1.void unsetCell(int x, int y, int z)Sets the value atmatrix[x][y][z]to 0.int largestMatrix()Returns the indexxwherematrix[x]contains the most number of 1's. If there are multiple such indices, return the largestx.
Example 1:
Input:
["Matrix3D", "setCell", "largestMatrix", "setCell", "largestMatrix", "setCell", "largestMatrix"]
[[3], [0, 0, 0], [], [1, 1, 2], [], [0, 0, 1], []]
Output:
[null, null, 0, null, 1, null, 0]
Explanation
Matrix3D matrix3D = new Matrix3D(3); // Initializes a 3 x 3 x 3 3D array matrix, filled with all 0's.
matrix3D.setCell(0, 0, 0); // Sets matrix[0][0][0] to 1.
matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
matrix3D.setCell(1, 1, 2); // Sets matrix[1][1][2] to 1.
matrix3D.largestMatrix(); // Returns 1. matrix[0] and matrix[1] tie with the most number of 1's, but index 1 is bigger.
matrix3D.setCell(0, 0, 1); // Sets matrix[0][0][1] to 1.
matrix3D.largestMatrix(); // Returns 0. matrix[0] has the most number of 1's.
Example 2:
Input:
["Matrix3D", "setCell", "largestMatrix", "unsetCell", "largestMatrix"]
[[4], [2, 1, 1], [], [2, 1, 1], []]
Output:
[null, null, 2, null, 3]
Explanation
Matrix3D matrix3D = new Matrix3D(4); // Initializes a 4 x 4 x 4 3D array matrix, filled with all 0's.
matrix3D.setCell(2, 1, 1); // Sets matrix[2][1][1] to 1.
matrix3D.largestMatrix(); // Returns 2. matrix[2] has the most number of 1's.
matrix3D.unsetCell(2, 1, 1); // Sets matrix[2][1][1] to 0.
matrix3D.largestMatrix(); // Returns 3. All indices from 0 to 3 tie with the same number of 1's, but index 3 is the biggest.
Constraints:
1 <= n <= 1000 <= x, y, z < n- At most
10^5calls are made in total tosetCellandunsetCell. - At most
10^4calls are made tolargestMatrix.
Solution
Approach: Per-layer counters
Store the cube in a 3D array g and keep cnt[x], the number of ones in layer x. setCell and unsetCell only change a cell (and cnt[x]) when its value actually flips, so repeated calls are harmless.
largestMatrix scans the n counters and returns the last index with the maximum count. Because n <= 100 and there are at most 10^4 such calls, the scan is cheap. (A balanced tree keyed by (count, layer) would make it O(1), but is not needed here.) With all counters at zero, the scan naturally returns n - 1.
Complexity: setCell and unsetCell take O(1), largestMatrix takes O(n); space O(n^3).
class Matrix3D:
def __init__(self, n: int):
self.n = n
self.g = [[[0] * n for _ in range(n)] for _ in range(n)]
self.cnt = [0] * n
def setCell(self, x: int, y: int, z: int) -> None:
if self.g[x][y][z] == 0:
self.g[x][y][z] = 1
self.cnt[x] += 1
def unsetCell(self, x: int, y: int, z: int) -> None:
if self.g[x][y][z] == 1:
self.g[x][y][z] = 0
self.cnt[x] -= 1
def largestMatrix(self) -> int:
ans = 0
for x in range(self.n):
if self.cnt[x] >= self.cnt[ans]:
ans = x
return ans
# Your Matrix3D object will be instantiated and called as such:
# obj = Matrix3D(n)
# obj.setCell(x,y,z)
# obj.unsetCell(x,y,z)
# param_3 = obj.largestMatrix()