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)$.
fromsortedcontainersimportSortedListclassmatrix3D:def__init__(self,n:int):self.g=[[[0]*nfor_inrange(n)]for_inrange(n)]self.cnt=[0]*nself.sl=SortedList(key=lambdax:(-x[0],-x[1]))defsetCell(self,x:int,y:int,z:int)->None:ifself.g[x][y][z]:returnself.g[x][y][z]=1self.sl.discard((self.cnt[x],x))self.cnt[x]+=1self.sl.add((self.cnt[x],x))defunsetCell(self,x:int,y:int,z:int)->None:ifself.g[x][y][z]==0:returnself.g[x][y][z]=0self.sl.discard((self.cnt[x],x))self.cnt[x]-=1ifself.cnt[x]:self.sl.add((self.cnt[x],x))deflargestMatrix(self)->int:returnself.sl[0][1]ifself.slelselen(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()
classmatrix3D{privatefinalint[][][]g;privatefinalint[]cnt;privatefinalTreeSet<int[]>sl=newTreeSet<>((a,b)->a[0]==b[0]?b[1]-a[1]:b[0]-a[0]);publicmatrix3D(intn){g=newint[n][n][n];cnt=newint[n];}publicvoidsetCell(intx,inty,intz){if(g[x][y][z]==1){return;}g[x][y][z]=1;sl.remove(newint[]{cnt[x],x});cnt[x]++;sl.add(newint[]{cnt[x],x});}publicvoidunsetCell(intx,inty,intz){if(g[x][y][z]==0){return;}g[x][y][z]=0;sl.remove(newint[]{cnt[x],x});cnt[x]--;if(cnt[x]>0){sl.add(newint[]{cnt[x],x});}}publicintlargestMatrix(){returnsl.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(); */
classmatrix3D{private:vector<vector<vector<int>>>g;vector<int>cnt;set<pair<int,int>>sl;public:matrix3D(intn){g.resize(n,vector<vector<int>>(n,vector<int>(n,0)));cnt.resize(n,0);}voidsetCell(intx,inty,intz){if(g[x][y][z]==1){return;}g[x][y][z]=1;sl.erase({-cnt[x],-x});cnt[x]++;sl.insert({-cnt[x],-x});}voidunsetCell(intx,inty,intz){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});}}intlargestMatrix(){returnsl.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(); */