-
Notifications
You must be signed in to change notification settings - Fork 1
840. Magic Squares In Grid #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ac3df1
840. Magic Squares In Grid solution with php
tanhongit 27d9292
change the name for magic squares in gird solution
tanhongit 41981b9
Update solution-php.md
tanhongit d9d917b
Update solution-php.md
tanhongit 65f20f3
Update solution-php.md spiral matrix III
tanhongit 0b9e2d6
docs: 840. Magic Squares In Grid
tanhongit fa68f43
Merge remote-tracking branch 'origin-tan/main' into main-tan
tanhongit 3be2569
fix: update descriptions
tanhongit 3bfe003
fix: update assets
tanhongit b2ad759
Create fork.yml
tanhongit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: 'Upstream Sync' | ||
|
||
on: | ||
push: | ||
branches: | ||
- '*' | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
sync_latest_from_upstream: | ||
runs-on: ubuntu-latest | ||
name: Sync latest commits from upstream repo | ||
|
||
steps: | ||
- name: Checkout target repo | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: main | ||
|
||
- name: Sync upstream changes | ||
id: sync | ||
uses: aormsby/Fork-Sync-With-Upstream-action@v3.4.1 | ||
with: | ||
target_sync_branch: main | ||
target_repo_token: ${{ secrets.GITHUB_TOKEN }} | ||
upstream_sync_branch: main | ||
upstream_sync_repo: Algorithms-and-Chickens/leetcode | ||
upstream_repo_access_token: ${{ secrets.UPSTREAM_REPO_SECRET }} | ||
|
||
- name: New commits found | ||
if: steps.sync.outputs.has_new_commits == 'true' | ||
run: echo "New commits were found to sync." | ||
|
||
- name: No new commits | ||
if: steps.sync.outputs.has_new_commits == 'false' | ||
run: echo "There were no new commits." | ||
|
||
- name: Show value of 'has_new_commits' | ||
run: echo ${{ steps.sync.outputs.has_new_commits }} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
URL: [840. Magic Squares In Grid | ||
](https://leetcode.com/problems/magic-squares-in-grid/description/) | ||
|
||
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. | ||
|
||
Given a `row x col` `grid` of integers, how many `3 x 3` contiguous magic square subgrids are there? | ||
|
||
Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15. | ||
|
||
 | ||
|
||
**Example 1:** | ||
|
||
|
||
- Input: `grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]` | ||
- Output: 1 | ||
- Explanation: The following subgrid is a `3 x 3` magic square: | ||
|
||
 | ||
|
||
while this one is not: | ||
|
||
 | ||
- | ||
In total, there is only one magic square inside the given grid. | ||
|
||
**Example 2:** | ||
|
||
> Input: `grid = [[8]]` | ||
> | ||
> Output: `0` | ||
|
||
|
||
**Constraints:** | ||
|
||
- `row == grid.length` | ||
- `col == grid[i].length` | ||
- `1 <= row, col <= 10` | ||
- `0 <= grid[i][j] <= 15` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Solution URL | ||
|
||
https://leetcode.com/discuss/topic/5611092/solution-100-beats-in-php-with-explanation/ | ||
|
||
# Intuition | ||
|
||
To determine how many `3x3` subgrids within a given grid are magic squares, we need to check each possible `3x3` subgrid. | ||
|
||
**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. | ||
|
||
# Approach | ||
|
||
1. **Grid Size Check**: | ||
|
||
First, we check if the grid has fewer than 3 rows or columns. | ||
|
||
If it does, return 0 because no `3x3` subgrid can exist. | ||
|
||
2. **Iterate Over Subgrids**: | ||
Use nested loops to iterate over all possible `3x3` subgrids in the grid. | ||
|
||
3. **Helper Function**: | ||
For each subgrid, use a helper function to: | ||
- Ensure all numbers from 1 to 9 are present exactly once. | ||
- Check if the sums of all rows, columns, and diagonals are equal. | ||
|
||
4. **Count Valid Subgrids**: | ||
Increment a counter for each valid magic square subgrid found. | ||
If the helper function returns true, increment the counter `$res`. | ||
|
||
# Complexity | ||
- **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. | ||
|
||
- **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. | ||
|
||
# Code | ||
```php | ||
class Solution { | ||
/** | ||
* @param Integer[][] $grid | ||
* @return Integer | ||
*/ | ||
public static function numMagicSquaresInside($grid) { | ||
$m = count($grid); | ||
$n = count($grid[0]); | ||
$res = 0; | ||
if ($m < 3 || $n < 3) { | ||
return 0; | ||
} | ||
|
||
for ($row = 0; $row <= $m - 3; $row++) { | ||
for ($col = 0; $col <= $n - 3; $col++) { | ||
$set = array_fill(0, 10, 0); | ||
if (self::helper($grid, $row, $col, $set)) { | ||
$res++; | ||
} | ||
} | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* @param Integer[][] $grid | ||
* @param Integer $row | ||
* @param Integer $col | ||
* @param Integer[] $set | ||
* @return Boolean | ||
*/ | ||
private static function helper($grid, $row, $col, &$set) { | ||
for ($i = 0; $i < 3; $i++) { | ||
for ($j = 0; $j < 3; $j++) { | ||
$val = $grid[$row + $i][$col + $j]; | ||
if ($val > 9 || $val < 1 || $set[$val] > 0) { | ||
return false; | ||
} | ||
$set[$val]++; | ||
} | ||
} | ||
|
||
$sum_row1 = $grid[$row][$col] + $grid[$row][$col + 1] + $grid[$row][$col + 2]; | ||
$sum_row2 = $grid[$row + 1][$col] + $grid[$row + 1][$col + 1] + $grid[$row + 1][$col + 2]; | ||
$sum_row3 = $grid[$row + 2][$col] + $grid[$row + 2][$col + 1] + $grid[$row + 2][$col + 2]; | ||
|
||
if ($sum_row1 != $sum_row2 || $sum_row1 != $sum_row3) { | ||
return false; | ||
} | ||
|
||
$sum_col1 = $grid[$row][$col] + $grid[$row + 1][$col] + $grid[$row + 2][$col]; | ||
$sum_col2 = $grid[$row][$col + 1] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col + 1]; | ||
$sum_col3 = $grid[$row][$col + 2] + $grid[$row + 1][$col + 2] + $grid[$row + 2][$col + 2]; | ||
|
||
if ($sum_col1 != $sum_col2 || $sum_col1 != $sum_col3) { | ||
return false; | ||
} | ||
|
||
$diag1 = $grid[$row][$col] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col + 2]; | ||
$diag2 = $grid[$row][$col + 2] + $grid[$row + 1][$col + 1] + $grid[$row + 2][$col]; | ||
|
||
if ($diag1 != $diag2) { | ||
return false; | ||
} | ||
|
||
if ($sum_col1 != $sum_row1 || $sum_row1 != $diag1) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
``` | ||
|
||
If it can help you, please give me an upvote. Thank you so much for reading! |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check this solution