Skip to content

Commit 4ac3df1

Browse files
committed
840. Magic Squares In Grid solution with php
1 parent ae5151e commit 4ac3df1

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Intuition
2+
3+
To determine how many `3x3` subgrids within a given grid are magic squares, we need to check each possible `3x3` subgrid.
4+
5+
**A magic square** is defined as a grid where the sums of rows, columns, and diagonals are all equal and contain the numbers from 1 to 9 exactly once.
6+
7+
# Approach
8+
9+
1. **Grid Size Check**:
10+
11+
First, we check if the grid has fewer than 3 rows or columns.
12+
13+
If it does, return 0 because no `3x3` subgrid can exist.
14+
15+
2. **Iterate Over Subgrids**:
16+
Use nested loops to iterate over all possible `3x3` subgrids in the grid.
17+
18+
3. **Helper Function**:
19+
For each subgrid, use a helper function to:
20+
- Ensure all numbers from 1 to 9 are present exactly once.
21+
- Check if the sums of all rows, columns, and diagonals are equal.
22+
23+
4. **Count Valid Subgrids**:
24+
Increment a counter for each valid magic square subgrid found.
25+
If the helper function returns true, increment the counter `$res`.
26+
27+
# Complexity
28+
- **Time complexity**:
29+
30+
`O(m×n)` is the number of rows and (n) is the number of columns in the grid. We iterate over each possible `3x3` subgrid and check if it is a magic square.
31+
32+
- **Space complexity**:
33+
34+
`O(1)`, aside from the input grid, as we use a constant amount of extra space to store the set of numbers and the sums.
35+
36+
# Code
37+
```php
38+
class Solution {
39+
/**
40+
* @param Integer[][] $grid
41+
* @return Integer
42+
*/
43+
public static function numMagicSquaresInside($grid) {
44+
$m = count($grid);
45+
$n = count($grid[0]);
46+
$res = 0;
47+
if ($m < 3 || $n < 3) {
48+
return 0;
49+
}
50+
51+
for ($row = 0; $row <= $m - 3; $row++) {
52+
for ($col = 0; $col <= $n - 3; $col++) {
53+
$set = array_fill(0, 10, 0);
54+
if (self::helper($grid, $row, $col, $set)) {
55+
$res++;
56+
}
57+
}
58+
}
59+
60+
return $res;
61+
}
62+
63+
/**
64+
* @param Integer[][] $grid
65+
* @param Integer $row
66+
* @param Integer $col
67+
* @param Integer[] $set
68+
* @return Boolean
69+
*/
70+
private static function helper($grid, $row, $col, &$set) {
71+
for ($i = 0; $i < 3; $i++) {
72+
for ($j = 0; $j < 3; $j++) {
73+
$val = $grid[$row + $i][$col + $j];
74+
if ($val > 9 || $val < 1 || $set[$val] > 0) {
75+
return false;
76+
}
77+
$set[$val]++;
78+
}
79+
}
80+
81+
$sum_row1 = $grid[$row][$col] + $grid[$row][$col + 1] + $grid[$row][$col + 2];
82+
$sum_row2 = $grid[$row + 1][$col] + $grid[$row + 1][$col + 1] + $grid[$row + 1][$col + 2];
83+
$sum_row3 = $grid[$row + 2][$col] + $grid[$row + 2][$col + 1] + $grid[$row + 2][$col + 2];
84+
85+
if ($sum_row1 != $sum_row2 || $sum_row1 != $sum_row3) {
86+
return false;
87+
}
88+
89+
$sum_col1 = $grid[$row][$col] + $grid[$row + 1][$col] + $grid[$row + 2][$col];
90+
$sum_col2 = $grid[$row][$col + 1] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col + 1];
91+
$sum_col3 = $grid[$row][$col + 2] + $grid[$row + 1][$col + 2] + $grid[$row + 2][$col + 2];
92+
93+
if ($sum_col1 != $sum_col2 || $sum_col1 != $sum_col3) {
94+
return false;
95+
}
96+
97+
$diag1 = $grid[$row][$col] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col + 2];
98+
$diag2 = $grid[$row][$col + 2] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col];
99+
100+
if ($diag1 != $diag2) {
101+
return false;
102+
}
103+
104+
if ($sum_col1 != $sum_row1 || $sum_row1 != $diag1) {
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
}
111+
```
112+
113+
If it can help you, please give me a upvote. Thank you so for reading!

0 commit comments

Comments
 (0)