leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 80048f468eba5a4d8a1b3ca1d82731fe5c012999
parent ae5a9173125eb2af2d51eed32e401600aa070da6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri,  9 Aug 2024 21:30:38 +0200

Daily Problem

Diffstat:
AProblems/0840.cpp | 30++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/Problems/0840.cpp b/Problems/0840.cpp @@ -0,0 +1,30 @@ +class Solution { + public: + int numMagicSquaresInside(const vector<vector<int>> &grid) const { + const int n = size(grid), m = size(grid[0]); + int res = 0; + + for (int i = 1; i < n - 1; i++) { + for (int j = 1; j < m - 1; j++) { + if (grid[i][j] != 5) continue; + + if (grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] != 15) continue; + if (grid[i - 1][j - 1] + grid[i][j - 1] + grid[i + 1][j - 1] != 15) continue; + + unordered_set<int> us; + for (int k = i - 1; k <= i + 1; k++) { + for (int l = j - 1; l <= j + 1; l++) { + if (grid[k][l] < 1 || grid[k][l] > 9) goto next; + if (us.count(grid[k][l])) goto next; + us.insert(grid[k][l]); + } + } + + res++; + next:; + } + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -525,6 +525,7 @@ for solving problems. | 0837 | Medium | [New 21 Game](Problems/0837.cpp) | | 0838 | Medium | [Push Dominoes](Problems/0838.cpp) | | 0839 | Hard | [Similar String Groups](Problems/0839.cpp) | +| 0840 | Medium | [Magic Squares In Grid](Problems/0840.cpp) | | 0841 | Medium | [Keys and Rooms](Problems/0841.cpp) | | 0844 | Easy | [Backspace String Compare](Problems/0844.cpp) | | 0846 | Medium | [Hand of Straights](Problems/0846.cpp) |