Gegeben eine n × n binäre Matrix zusammen mit bestehend aus 0er Und 1s . Ihre Aufgabe ist es, die Größe des größten zu ermitteln '+' Form, die nur mit gebildet werden kann 1s .
A '+' Die Form besteht aus einer Mittelzelle mit vier Armen, die sich in alle vier Richtungen erstrecken ( oben unten links und rechts ) und bleibt dabei innerhalb der Matrixgrenzen. Die Größe eines '+' ist definiert als Gesamtzahl der Zellen Formen Sie es einschließlich der Mitte und aller Arme.
Die Aufgabe besteht darin, die zurückzugeben maximale Größe von jedem gültigen '+' In zusammen mit . Wenn nein '+' kann gebildet werden zurück .
Beispiele:
Eingang: mit = [ [0 1 1 0 1] [0 0 1 1 1] [1 1 1 1 1] [1 1 1 0 1] [0 1 1 1 0] ]
Ausgabe: 9
Erläuterung: In der Mitte der Matte kann ein „+“ mit einer Armlänge von 2 (2 Zellen in jede Richtung + 1 Mitte) gebildet werden.
0 1 1 0 1
0 0 1 1 1
1 1 1 1 1
1 1 1 0 1
0 1 1 1 0
Gesamtgröße = (2 × 4) + 1 = 9Eingang: mit = [ [0 1 1] [0 0 1] [1 1 1] ]
Ausgabe: 1
Erläuterung: Ein „+“ mit einer Armlänge von 0 (0 Zellen in jede Richtung + 1 Mittelpunkt) kann mit jeder der Einsen gebildet werden.Fragen zum Java-InterviewEingang: mit = [ [0] ]
Ausgabe:
Erläuterung: NEIN Es kann ein „+“-Zeichen gebildet werden.
[Naiver Ansatz] – Betrachten Sie jeden Punkt als Zentrum – O(n^4) Zeit und O(n^4) Raum
Durchlaufen Sie die Matrixzellen nacheinander. Betrachten Sie jeden durchlaufenen Punkt als Mittelpunkt eines Pluspunktes und ermitteln Sie die Größe des +. Für jedes Element durchlaufen wir links, rechts, unten und oben. Der schlimmste Fall in dieser Lösung tritt ein, wenn wir alle Einsen haben.
[Erwarteter Ansatz] – 4 Arrays vorab berechnen – O(n^2) Zeit und O(n^2) Raum
Der Idee besteht darin, vier Hilfsmatrizen zu verwalten links[][] rechts[][] oben[][] unten[][] um aufeinanderfolgende Einsen in jede Richtung zu speichern. Für jede Zelle (i j) In der Eingabematrix hinterlegen wir nachfolgend Informationen zu diesen vier Matrizen -
- links(i j) speichert die maximale Anzahl aufeinanderfolgender Einsen links der Zelle (i j) einschließlich Zelle (i j).
- richtig(i j) speichert die maximale Anzahl aufeinanderfolgender Einsen Rechts der Zelle (i j) einschließlich Zelle (i j).
- oben(i j) speichert die maximale Anzahl aufeinanderfolgender Einsen unter Spitze der Zelle (i j) einschließlich Zelle (i j).
- unten(i j) speichert die maximale Anzahl aufeinanderfolgender Einsen unter unten der Zelle (i j) einschließlich Zelle (i j).
Nach der Berechnung des Werts für jede Zelle der oben genannten Matrizen größtes'+' würde durch eine Zelle der Eingabematrix gebildet werden, die unter Berücksichtigung des Minimums von ( links(i j) rechts(i j) oben(i j) unten(i j) )
Wir können verwenden Dynamische Programmierung So berechnen Sie die Gesamtzahl der aufeinanderfolgenden Einsen in jede Richtung:
wenn mat(i j) == 1
links(i j) = links(i j - 1) + 1sonst left(i j) = 0
wenn mat(i j) == 1
top(i j) = top(i - 1 j) + 1;sonst top(i j) = 0;
wenn mat(i j) == 1
unten(i j) = unten(i + 1 j) + 1;sonst unten(i j) = 0;
wenn mat(i j) == 1sonst right(i j) = 0;
Nachfolgend finden Sie die Umsetzung des oben genannten Ansatzes:
C++// C++ program to find the largest '+' in a binary matrix // using Dynamic Programming #include using namespace std; int findLargestPlus(vector<vector<int>> &mat) { int n = mat.size(); vector<vector<int>> left(n vector<int>(n 0)); vector<vector<int>> right(n vector<int>(n 0)); vector<vector<int>> top(n vector<int>(n 0)); vector<vector<int>> bottom(n vector<int>(n 0)); // Fill left and top matrices for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i][j] == 1) { left[i][j] = (j == 0) ? 1 : left[i][j - 1] + 1; top[i][j] = (i == 0) ? 1 : top[i - 1][j] + 1; } } } // Fill right and bottom matrices for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (mat[i][j] == 1) { right[i][j] = (j == n - 1) ? 1 : right[i][j + 1] + 1; bottom[i][j] = (i == n - 1) ? 1 : bottom[i + 1][j] + 1; } } } int maxPlusSize = 0; // Compute the maximum '+' size for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i][j] == 1) { int armLength = min({left[i][j] right[i][j] top[i][j] bottom[i][j]}); maxPlusSize = max(maxPlusSize (4 * (armLength - 1)) + 1); } } } return maxPlusSize; } int main() { // Hardcoded input matrix vector<vector<int>> mat = { {0 1 1 0 1} {0 0 1 1 1} {1 1 1 1 1} {1 1 1 0 1} {0 1 1 1 0} }; cout << findLargestPlus(mat) << endl; return 0; }
Java // Java program to find the largest '+' in a binary matrix // using Dynamic Programming class GfG { static int findLargestPlus(int[][] mat) { int n = mat.length; int[][] left = new int[n][n]; int[][] right = new int[n][n]; int[][] top = new int[n][n]; int[][] bottom = new int[n][n]; // Fill left and top matrices for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i][j] == 1) { left[i][j] = (j == 0) ? 1 : left[i][j - 1] + 1; top[i][j] = (i == 0) ? 1 : top[i - 1][j] + 1; } } } // Fill right and bottom matrices for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (mat[i][j] == 1) { right[i][j] = (j == n - 1) ? 1 : right[i][j + 1] + 1; bottom[i][j] = (i == n - 1) ? 1 : bottom[i + 1][j] + 1; } } } int maxPlusSize = 0; // Compute the maximum '+' size for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i][j] == 1) { int armLength = Math.min(Math.min(left[i][j] right[i][j]) Math.min(top[i][j] bottom[i][j])); maxPlusSize = Math.max(maxPlusSize (4 * (armLength - 1)) + 1); } } } return maxPlusSize; } public static void main(String[] args) { // Hardcoded input matrix int[][] mat = { {0 1 1 0 1} {0 0 1 1 1} {1 1 1 1 1} {1 1 1 0 1} {0 1 1 1 0} }; System.out.println(findLargestPlus(mat)); } }
Python # Python program to find the largest '+' in a binary matrix # using Dynamic Programming def findLargestPlus(mat): n = len(mat) left = [[0] * n for i in range(n)] right = [[0] * n for i in range(n)] top = [[0] * n for i in range(n)] bottom = [[0] * n for i in range(n)] # Fill left and top matrices for i in range(n): for j in range(n): if mat[i][j] == 1: left[i][j] = 1 if j == 0 else left[i][j - 1] + 1 top[i][j] = 1 if i == 0 else top[i - 1][j] + 1 # Fill right and bottom matrices for i in range(n - 1 -1 -1): for j in range(n - 1 -1 -1): if mat[i][j] == 1: right[i][j] = 1 if j == n - 1 else right[i][j + 1] + 1 bottom[i][j] = 1 if i == n - 1 else bottom[i + 1][j] + 1 maxPlusSize = 0 # Compute the maximum '+' size for i in range(n): for j in range(n): if mat[i][j] == 1: armLength = min(left[i][j] right[i][j] top[i][j] bottom[i][j]) maxPlusSize = max(maxPlusSize (4 * (armLength - 1)) + 1) return maxPlusSize if __name__ == '__main__': # Hardcoded input matrix mat = [ [0 1 1 0 1] [0 0 1 1 1] [1 1 1 1 1] [1 1 1 0 1] [0 1 1 1 0] ] print(findLargestPlus(mat))
C# // C# program to find the largest '+' in a binary matrix // using Dynamic Programming using System; class GfG { static int FindLargestPlus(int[] mat) { int n = mat.GetLength(0); int[] left = new int[n n]; int[] right = new int[n n]; int[] top = new int[n n]; int[] bottom = new int[n n]; // Fill left and top matrices for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i j] == 1) { left[i j] = (j == 0) ? 1 : left[i j - 1] + 1; top[i j] = (i == 0) ? 1 : top[i - 1 j] + 1; } } } // Fill right and bottom matrices for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (mat[i j] == 1) { right[i j] = (j == n - 1) ? 1 : right[i j + 1] + 1; bottom[i j] = (i == n - 1) ? 1 : bottom[i + 1 j] + 1; } } } int maxPlusSize = 0; // Compute the maximum '+' size for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mat[i j] == 1) { int armLength = Math.Min(Math.Min(left[i j] right[i j]) Math.Min(top[i j] bottom[i j])); maxPlusSize = Math.Max(maxPlusSize (4 * (armLength - 1)) + 1); } } } return maxPlusSize; } public static void Main() { // Hardcoded input matrix int[] mat = { {0 1 1 0 1} {0 0 1 1 1} {1 1 1 1 1} {1 1 1 0 1} {0 1 1 1 0} }; Console.WriteLine(FindLargestPlus(mat)); } }
JavaScript // JavaScript program to find the largest '+' in a binary matrix // using Dynamic Programming function findLargestPlus(mat) { let n = mat.length; let left = Array.from({ length: n } () => Array(n).fill(0)); let right = Array.from({ length: n } () => Array(n).fill(0)); let top = Array.from({ length: n } () => Array(n).fill(0)); let bottom = Array.from({ length: n } () => Array(n).fill(0)); // Fill left and top matrices for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { if (mat[i][j] === 1) { left[i][j] = (j === 0) ? 1 : left[i][j - 1] + 1; top[i][j] = (i === 0) ? 1 : top[i - 1][j] + 1; } } } // Fill right and bottom matrices for (let i = n - 1; i >= 0; i--) { for (let j = n - 1; j >= 0; j--) { if (mat[i][j] === 1) { right[i][j] = (j === n - 1) ? 1 : right[i][j + 1] + 1; bottom[i][j] = (i === n - 1) ? 1 : bottom[i + 1][j] + 1; } } } let maxPlusSize = 0; // Compute the maximum '+' size for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { if (mat[i][j] === 1) { let armLength = Math.min(left[i][j] right[i][j] top[i][j] bottom[i][j]); maxPlusSize = Math.max(maxPlusSize (4 * (armLength - 1)) + 1); } } } return maxPlusSize; } // Hardcoded input matrix let mat = [ [0 1 1 0 1] [0 0 1 1 1] [1 1 1 1 1] [1 1 1 0 1] [0 1 1 1 0] ]; console.log(findLargestPlus(mat));
Ausgabe
9
Zeitkomplexität: O(n²) aufgrund von vier Durchgängen zur Berechnung der Richtungsmatrizen und einem letzten Durchgang zur Bestimmung des größten „+“. Jeder Durchgang benötigt O(n²) Zeit, was zu einer Gesamtkomplexität von O(n²) führt.
Raumkomplexität: O(n²) da vier Hilfsmatrizen (links rechts oben unten) O(n²) zusätzlichen Platz beanspruchen.