Welcome to Subscribe On Youtube
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 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
.
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)$.
-
class matrix3D { private final int[][][] g; private final int[] cnt; private final TreeSet<int[]> sl = new TreeSet<>((a, b) -> a[0] == b[0] ? b[1] - a[1] : b[0] - a[0]); public matrix3D(int n) { g = new int[n][n][n]; cnt = new int[n]; } public void setCell(int x, int y, int z) { if (g[x][y][z] == 1) { return; } g[x][y][z] = 1; sl.remove(new int[] {cnt[x], x}); cnt[x]++; sl.add(new int[] {cnt[x], x}); } public void unsetCell(int x, int y, int z) { if (g[x][y][z] == 0) { return; } g[x][y][z] = 0; sl.remove(new int[] {cnt[x], x}); cnt[x]--; if (cnt[x] > 0) { sl.add(new int[] {cnt[x], x}); } } public int largestMatrix() { return sl.isEmpty() ? g.length - 1 : sl.first()[1]; } } /** * Your matrix3D object will be instantiated and called as such: * matrix3D obj = new matrix3D(n); * obj.setCell(x,y,z); * obj.unsetCell(x,y,z); * int param_3 = obj.largestMatrix(); */
-
class matrix3D { private: vector<vector<vector<int>>> g; vector<int> cnt; set<pair<int, int>> sl; public: matrix3D(int n) { g.resize(n, vector<vector<int>>(n, vector<int>(n, 0))); cnt.resize(n, 0); } void setCell(int x, int y, int z) { if (g[x][y][z] == 1) { return; } g[x][y][z] = 1; sl.erase({-cnt[x], -x}); cnt[x]++; sl.insert({-cnt[x], -x}); } void unsetCell(int x, int y, int z) { if (g[x][y][z] == 0) { return; } g[x][y][z] = 0; sl.erase({-cnt[x], -x}); cnt[x]--; if (cnt[x]) { sl.insert({-cnt[x], -x}); } } int largestMatrix() { return sl.empty() ? g.size() - 1 : -sl.begin()->second; } }; /** * Your matrix3D object will be instantiated and called as such: * matrix3D* obj = new matrix3D(n); * obj->setCell(x,y,z); * obj->unsetCell(x,y,z); * int param_3 = obj->largestMatrix(); */
-
from sortedcontainers import SortedList class matrix3D: def __init__(self, n: int): self.g = [[[0] * n for _ in range(n)] for _ in range(n)] self.cnt = [0] * n self.sl = SortedList(key=lambda x: (-x[0], -x[1])) def setCell(self, x: int, y: int, z: int) -> None: if self.g[x][y][z]: return self.g[x][y][z] = 1 self.sl.discard((self.cnt[x], x)) self.cnt[x] += 1 self.sl.add((self.cnt[x], x)) def unsetCell(self, x: int, y: int, z: int) -> None: if self.g[x][y][z] == 0: return self.g[x][y][z] = 0 self.sl.discard((self.cnt[x], x)) self.cnt[x] -= 1 if self.cnt[x]: self.sl.add((self.cnt[x], x)) def largestMatrix(self) -> int: return self.sl[0][1] if self.sl else len(self.g) - 1 # 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()