3391. Design a 3D Binary Matrix with Efficient Layer Tracking 🔒
题目描述
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 indexx
wherematrix[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 <= 100
0 <= x, y, z < n
- At most
105
calls are made in total tosetCell
andunsetCell
. - At most
104
calls are made tolargestMatrix
.
解法
方法一:计数 + 有序集合
我们使用一个三维数组 $\textit{g}$ 来表示矩阵,其中 $\textit{g}[x][y][z]$ 表示矩阵中坐标 $(x, y, z)$ 的值,用一个长度为 $n$ 的数组 $\textit{cnt}$ 来记录每一层的 $1$ 的个数,用一个有序集合 $\textit{sl}$ 来维护每一层的 $1$ 的个数和层数,其中 $\textit{sl}$ 中的元素是 $(\textit{cnt}[x], x)$,这样 $\textit{sl}$ 就能按照 $1$ 的个数降序排序,如果 $1$ 的个数相同,则按照层数降序排序。
调用 setCell
方法时,我们先判断 $(x, y, z)$ 是否已经被设置为 $1$,如果是则直接返回,否则将 $\textit{g}[x][y][z]$ 设置为 $1$,然后将 $(\textit{cnt}[x], x)$ 从 $\textit{sl}$ 中删除,将 $\textit{cnt}[x]$ 加一,再将 $(\textit{cnt}[x], x)$ 加入 $\textit{sl}$。
调用 unsetCell
方法时,我们先判断 $(x, y, z)$ 是否已经被设置为 $0$,如果是则直接返回,否则将 $\textit{g}[x][y][z]$ 设置为 $0$,然后将 $(\textit{cnt}[x], x)$ 从 $\textit{sl}$ 中删除,将 $\textit{cnt}[x]$ 减一,如果 $\textit{cnt}[x]$ 大于 $0$,则将 $(\textit{cnt}[x], x)$ 加入 $\textit{sl}$。
调用 largestMatrix
方法时,我们返回 $\textit{sl}$ 中第一个元素的第二个值,如果 $\textit{sl}$ 为空,则返回 $n - 1$。
时间复杂度方面,setCell
和 unsetCell
方法的时间复杂度均为 $O(\log n)$,largestMatrix
方法的时间复杂度为 $O(1)$。空间复杂度 $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 34 35 36 |
|
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 |
|