Skip to content

Commit c6cba75

Browse files
authored
Merge pull request #1 from tanhongit/main
840. Magic Squares In Grid
2 parents ae5151e + b2ad759 commit c6cba75

File tree

10 files changed

+206
-6
lines changed

10 files changed

+206
-6
lines changed

.github/workflows/fork.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: 'Upstream Sync'
2+
3+
on:
4+
push:
5+
branches:
6+
- '*'
7+
schedule:
8+
- cron: '0 0 * * *'
9+
10+
workflow_dispatch:
11+
12+
jobs:
13+
sync_latest_from_upstream:
14+
runs-on: ubuntu-latest
15+
name: Sync latest commits from upstream repo
16+
17+
steps:
18+
- name: Checkout target repo
19+
uses: actions/checkout@v4
20+
with:
21+
ref: main
22+
23+
- name: Sync upstream changes
24+
id: sync
25+
uses: aormsby/[email protected]
26+
with:
27+
target_sync_branch: main
28+
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
29+
upstream_sync_branch: main
30+
upstream_sync_repo: Algorithms-and-Chickens/leetcode
31+
upstream_repo_access_token: ${{ secrets.UPSTREAM_REPO_SECRET }}
32+
33+
- name: New commits found
34+
if: steps.sync.outputs.has_new_commits == 'true'
35+
run: echo "New commits were found to sync."
36+
37+
- name: No new commits
38+
if: steps.sync.outputs.has_new_commits == 'false'
39+
run: echo "There were no new commits."
40+
41+
- name: Show value of 'has_new_commits'
42+
run: echo ${{ steps.sync.outputs.has_new_commits }}
12.2 KB
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
URL: [840. Magic Squares In Grid
2+
](https://leetcode.com/problems/magic-squares-in-grid/description/)
3+
4+
A `3 x 3` magic square is a `3 x 3` grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.
5+
6+
Given a `row x col` `grid` of integers, how many `3 x 3` contiguous magic square subgrids are there?
7+
8+
Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15.
9+
10+
![img.png](assets/img.png)
11+
12+
**Example 1:**
13+
14+
15+
- Input: `grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]`
16+
- Output: 1
17+
- Explanation: The following subgrid is a `3 x 3` magic square:
18+
19+
![img_1.png](assets/img_1.png)
20+
21+
while this one is not:
22+
23+
![img_2.png](assets/img_2.png)
24+
-
25+
In total, there is only one magic square inside the given grid.
26+
27+
**Example 2:**
28+
29+
> Input: `grid = [[8]]`
30+
>
31+
> Output: `0`
32+
33+
34+
**Constraints:**
35+
36+
- `row == grid.length`
37+
- `col == grid[i].length`
38+
- `1 <= row, col <= 10`
39+
- `0 <= grid[i][j] <= 15`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Solution URL
2+
3+
https://leetcode.com/discuss/topic/5611092/solution-100-beats-in-php-with-explanation/
4+
5+
# Intuition
6+
7+
To determine how many `3x3` subgrids within a given grid are magic squares, we need to check each possible `3x3` subgrid.
8+
9+
**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.
10+
11+
# Approach
12+
13+
1. **Grid Size Check**:
14+
15+
First, we check if the grid has fewer than 3 rows or columns.
16+
17+
If it does, return 0 because no `3x3` subgrid can exist.
18+
19+
2. **Iterate Over Subgrids**:
20+
Use nested loops to iterate over all possible `3x3` subgrids in the grid.
21+
22+
3. **Helper Function**:
23+
For each subgrid, use a helper function to:
24+
- Ensure all numbers from 1 to 9 are present exactly once.
25+
- Check if the sums of all rows, columns, and diagonals are equal.
26+
27+
4. **Count Valid Subgrids**:
28+
Increment a counter for each valid magic square subgrid found.
29+
If the helper function returns true, increment the counter `$res`.
30+
31+
# Complexity
32+
- **Time complexity**: $O(\text{m} \times \text{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.
33+
34+
- **Space complexity**: $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 an upvote. Thank you so much for reading!
2.7 KB
Loading
6.42 KB
Loading

medium/885. Spiral Matrix III/description.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ Return an array of coordinates representing the positions of the grid in the ord
1111

1212
Example 1:
1313

14+
![img.png](assets/img.png)
15+
1416
> **Input:** rows = 1, cols = 4, rStart = 0, cStart = 0
1517
>
1618
> **Output:** [[0,0],[0,1],[0,2],[0,3]]
1719
1820
Example 2:
1921

22+
![img_1.png](assets/img_1.png)
23+
2024
> **Input:** rows = 5, cols = 6, rStart = 1, cStart = 4
2125
>
2226
> **Output:** [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
2327
2428

2529
Constraints:
2630

27-
- 1 <= rows, cols <= 100
28-
- 0 <= rStart < rows
29-
- 0 <= cStart < cols
31+
- `1 <= rows, cols <= 100`
32+
- `0 <= rStart < rows`
33+
- `0 <= cStart < cols`

medium/885. Spiral Matrix III/solution-php.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ https://leetcode.com/problems/spiral-matrix-iii/solutions/5606322/solution-beat-
44

55
# Intuition
66
<!-- Describe your first thoughts on how to solve this problem. -->
7-
- To traverse all the cells in a grid of size `rows * cols` starting from a specific position (`rStart`, `cStart`) in a spiral order, we can use an array of directions and a variable to keep track of the number of steps needed in each direction.
7+
- To traverse all the cells in a grid of size `rows * cols` starting from a specific position (`rStart`, `cStart`) in spiral order, we can use an array of directions and a variable to keep track of the number of steps needed in each direction.
88
- We will move in sequential directions: **right, down, left, and up**, increasing the number of steps after every two directions (one complete cycle).
99

1010
# Approach
@@ -17,11 +17,11 @@ https://leetcode.com/problems/spiral-matrix-iii/solutions/5606322/solution-beat-
1717
- After every two directions, increment the number of steps required.
1818

1919
# Complexity
20-
- Time complexity: **O(n×m)**
20+
- Time complexity: $O(n \times m)$
2121

2222
We will traverse all cells in the grid once, so the time complexity is linear with respect to the number of cells in the grid, or `O(n×m)`, where `n` is the number of rows and `m` is the number of columns.
2323

24-
- Space complexity: **O(n×m)**
24+
- Space complexity: $O(n \times m)$
2525

2626
The result array `ans` will contain all positions of the grid, so the space complexity is also linear with respect to the number of cells in the grid, or `O(n×m)`.
2727

@@ -63,3 +63,5 @@ class Solution
6363
}
6464

6565
```
66+
67+
If it can help you, please give me an upvote. Thank you so much for reading!

0 commit comments

Comments
 (0)