3391. Design a 3D Binary Matrix with Efficient Layer Tracking π
Description
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 a3 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 a4 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 
105calls are made in total tosetCellandunsetCell. - At most 
104calls are made tolargestMatrix. 
Solutions
Solution 1: Counting + Ordered Set
We use a three-dimensional array \(\textit{g}\) to represent the matrix, where \(\textit{g}[x][y][z]\) represents the value at coordinate \((x, y, z)\) in the matrix. We use an array \(\textit{cnt}\) of length \(n\) to record the number of 1s in each layer. We use an ordered set \(\textit{sl}\) to maintain the number of 1s and the layer number for each layer. The elements in \(\textit{sl}\) are \((\textit{cnt}[x], x)\), so \(\textit{sl}\) can be sorted in descending order by the number of 1s, and in descending order by layer number if the number of 1s is the same.
When calling the setCell method, we first check if \((x, y, z)\) has already been set to 1. If it has, we return directly. Otherwise, we set \(\textit{g}[x][y][z]\) to 1, remove \((\textit{cnt}[x], x)\) from \(\textit{sl}\), increment \(\textit{cnt}[x]\) by 1, and add \((\textit{cnt}[x], x)\) to \(\textit{sl}\).
When calling the unsetCell method, we first check if \((x, y, z)\) has already been set to 0. If it has, we return directly. Otherwise, we set \(\textit{g}[x][y][z]\) to 0, remove \((\textit{cnt}[x], x)\) from \(\textit{sl}\), decrement \(\textit{cnt}[x]\) by 1, and if \(\textit{cnt}[x]\) is greater than 0, add \((\textit{cnt}[x], x)\) to \(\textit{sl}\).
When calling the largestMatrix method, we return the second value of the first element in \(\textit{sl}\). If \(\textit{sl}\) is empty, we return \(n - 1\).
In terms of time complexity, the setCell and unsetCell methods both have a time complexity of \(O(\log n)\), and the largestMatrix method has a time complexity of \(O(1)\). The space complexity is \(O(n^3)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46  |  | 
1 |  |