Class Solution
java.lang.Object
g1801_1900.s1895_largest_magic_square.Solution
1895 - Largest Magic Square.<p>Medium</p>
<p>A <code>k x k</code> <strong>magic square</strong> is a <code>k x k</code> grid filled with integers such that every row sum, every column sum, and both diagonal sums are <strong>all equal</strong>. The integers in the magic square <strong>do not have to be distinct</strong>. Every <code>1 x 1</code> grid is trivially a <strong>magic square</strong>.</p>
<p>Given an <code>m x n</code> integer <code>grid</code>, return <em>the <strong>size</strong> (i.e., the side length</em> <code>k</code><em>) of the <strong>largest magic square</strong> that can be found within this grid</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" alt="" /></p>
<p><strong>Input:</strong> grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The largest magic square has a size of 3.</p>
<p>Every row sum, column sum, and diagonal sum of this magic square is equal to 12.</p>
<ul>
<li>
<p>Row sums: 5+1+6 = 5+4+3 = 2+7+3 = 12</p>
</li>
<li>
<p>Column sums: 5+5+2 = 1+4+7 = 6+3+3 = 12</p>
</li>
<li>
<p>Diagonal sums: 5+4+3 = 6+4+2 = 12</p>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" alt="" /></p>
<p><strong>Input:</strong> grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 10<sup>6</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
largestMagicSquare
public int largestMagicSquare(int[][] grid)
-