|
| 1 | +/** |
| 2 | + * [1895] Largest Magic Square |
| 3 | + * |
| 4 | + * A k x k magic square is a k x k grid filled with integers such that every row sum, every column sum, and both diagonal sums are all equal. The integers in the magic square do not have to be distinct. Every 1 x 1 grid is trivially a magic square. |
| 5 | + * Given an m x n integer grid, return the size (i.e., the side length k) of the largest magic square that can be found within this grid. |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" style="width: 413px; height: 335px;" /> |
| 9 | + * Input: grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]] |
| 10 | + * Output: 3 |
| 11 | + * Explanation: The largest magic square has a size of 3. |
| 12 | + * Every row sum, column sum, and diagonal sum of this magic square is equal to 12. |
| 13 | + * - Row sums: 5+1+6 = 5+4+3 = 2+7+3 = 12 |
| 14 | + * - Column sums: 5+5+2 = 1+4+7 = 6+3+3 = 12 |
| 15 | + * - Diagonal sums: 5+4+3 = 6+4+2 = 12 |
| 16 | + * |
| 17 | + * Example 2: |
| 18 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" style="width: 333px; height: 255px;" /> |
| 19 | + * Input: grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]] |
| 20 | + * Output: 2 |
| 21 | + * |
| 22 | + * |
| 23 | + * Constraints: |
| 24 | + * |
| 25 | + * m == grid.length |
| 26 | + * n == grid[i].length |
| 27 | + * 1 <= m, n <= 50 |
| 28 | + * 1 <= grid[i][j] <= 10^6 |
| 29 | + * |
| 30 | + */ |
| 31 | +pub struct Solution {} |
| 32 | + |
| 33 | +// problem: https://leetcode.com/problems/largest-magic-square/ |
| 34 | +// discuss: https://leetcode.com/problems/largest-magic-square/discuss/?currentPage=1&orderBy=most_votes&query= |
| 35 | + |
| 36 | +// submission codes start here |
| 37 | + |
| 38 | +impl Solution { |
| 39 | + pub fn largest_magic_square(grid: Vec<Vec<i32>>) -> i32 { |
| 40 | + 0 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// submission codes end |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use super::*; |
| 49 | + |
| 50 | + #[test] |
| 51 | + #[ignore] |
| 52 | + fn test_1895_example_1() { |
| 53 | + let grid = vec![ |
| 54 | + vec![7, 1, 4, 5, 6], |
| 55 | + vec![2, 5, 1, 6, 4], |
| 56 | + vec![1, 5, 4, 3, 2], |
| 57 | + vec![1, 2, 7, 3, 4], |
| 58 | + ]; |
| 59 | + |
| 60 | + let result = 3; |
| 61 | + |
| 62 | + assert_eq!(Solution::largest_magic_square(grid), result); |
| 63 | + } |
| 64 | + |
| 65 | + #[test] |
| 66 | + #[ignore] |
| 67 | + fn test_1895_example_2() { |
| 68 | + let grid = vec![vec![5, 1, 3, 1], vec![9, 3, 3, 1], vec![1, 3, 3, 8]]; |
| 69 | + |
| 70 | + let result = 2; |
| 71 | + |
| 72 | + assert_eq!(Solution::largest_magic_square(grid), result); |
| 73 | + } |
| 74 | +} |
0 commit comments