Skip to content
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

Solve Problem 840. Magic Squares In Grid #1630

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [823. Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors) | Medium | [C++](./problems/0823/solution.cpp) |
| [826. Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work/) | Medium | [C](./old-problems/0826/c/solution.c) [C++](./old-problems/0826/solution.cpp) |
| [834. Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | Hard | [C](./old-problems/0834/c/solution.c) [C++](./old-problems/0834/solution.cpp) |
| [840. Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | Medium | [C++](./old-problems/0840/solution.cpp) |
| [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare) | Easy | [C++](./problems/0844/solution.cpp) |
| [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/) | medium | [C](./old-problems/0846/c/solution.c) [C++](./old-problems/0846/solution.cpp) |
| [857. Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/) | Hard | [C++](./old-problems/0857/solution.cpp) |
Expand Down
2 changes: 2 additions & 0 deletions old-problems/0840/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
get_dir_name(id)
add_problem_test(test-${id} test.yaml)
79 changes: 79 additions & 0 deletions old-problems/0840/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <vector>

class Solution {
public:
int numMagicSquaresInside(std::vector<std::vector<int>>& grid) {
int num{0};
for (int i = grid.size() - 2; i > 0; --i) {
for (int j = grid[i].size() - 2; j > 0; --j) {
if (grid[i][j] != 5) continue;
switch (grid[i - 1][j - 1]) {
case 2:
switch (grid[i - 1][j]) {
case 9:
if (grid[i - 1][j + 1] == 4 && grid[i][j - 1] == 7 &&
grid[i][j + 1] == 3 && grid[i + 1][j - 1] == 6 &&
grid[i + 1][j] == 1 && grid[i + 1][j + 1] == 8) ++num;
break;

case 7:
if (grid[i - 1][j + 1] == 6 && grid[i][j - 1] == 9 &&
grid[i][j + 1] == 1 && grid[i + 1][j - 1] == 4 &&
grid[i + 1][j] == 3 && grid[i + 1][j + 1] == 8) ++num;
break;
}
break;

case 4:
switch (grid[i - 1][j]) {
case 3:
if (grid[i - 1][j + 1] == 8 && grid[i][j - 1] == 9 &&
grid[i][j + 1] == 1 && grid[i + 1][j - 1] == 2 &&
grid[i + 1][j] == 7 && grid[i + 1][j + 1] == 6) ++num;
break;

case 9:
if (grid[i - 1][j + 1] == 2 && grid[i][j - 1] == 3 &&
grid[i][j + 1] == 7 && grid[i + 1][j - 1] == 8 &&
grid[i + 1][j] == 1 && grid[i + 1][j + 1] == 6) ++num;
break;
}
break;

case 6:
switch (grid[i - 1][j]) {
case 7:
if (grid[i - 1][j + 1] == 2 && grid[i][j - 1] == 1 &&
grid[i][j + 1] == 9 && grid[i + 1][j - 1] == 8 &&
grid[i + 1][j] == 3 && grid[i + 1][j + 1] == 4) ++num;
break;

case 1:
if (grid[i - 1][j + 1] == 8 && grid[i][j - 1] == 7 &&
grid[i][j + 1] == 3 && grid[i + 1][j - 1] == 2 &&
grid[i + 1][j] == 9 && grid[i + 1][j + 1] == 4) ++num;
break;
}
break;

case 8:
switch (grid[i - 1][j]) {
case 1:
if (grid[i - 1][j + 1] == 6 && grid[i][j - 1] == 3 &&
grid[i][j + 1] == 7 && grid[i + 1][j - 1] == 4 &&
grid[i + 1][j] == 9 && grid[i + 1][j + 1] == 2) ++num;
break;

case 3:
if (grid[i - 1][j + 1] == 4 && grid[i][j - 1] == 1 &&
grid[i][j + 1] == 9 && grid[i + 1][j - 1] == 6 &&
grid[i + 1][j] == 7 && grid[i + 1][j + 1] == 2) ++num;
break;
}
break;
}
}
}
return num;
}
};
32 changes: 32 additions & 0 deletions old-problems/0840/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: 840. Magic Squares In Grid

types:
inputs:
grid: std::vector<std::vector<int>>
output: int

solutions:
cpp:
function: numMagicSquaresInside

test_cases:
example_1:
inputs:
grid:
- [4, 3, 8, 4]
- [9, 5, 1, 9]
- [2, 7, 6, 2]
output: 1

example_2:
inputs:
grid: [[8]]
output: 0

test_case_8:
inputs:
grid:
- [4, 7, 8]
- [9, 5, 1]
- [2, 3, 6]
output: 0