2023-04-07 Daily Challenge
Today I have done leetcode's April LeetCoding Challenge with cpp
.
April LeetCoding Challenge 7
Description
Number of Enclaves
You are given an m x n
binary matrix grid
, where 0
represents a sea cell and 1
represents a land cell.
A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid
.
Return the number of land cells in grid
for which we cannot walk off the boundary of the grid in any number of moves.
Example 1:
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] Output: 3 Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
Example 2:
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] Output: 0 Explanation: All 1s are either on the boundary or can reach the boundary.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 500
grid[i][j]
is either0
or1
.
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
const int MOVES[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
int numEnclaves(vector<vector<int>>& grid) {
queue<pair<int, int>> q;
int rows = grid.size();
int cols = grid.front().size();
vector<vector<bool>> visit(rows, vector<bool>(cols));
int answer = 0;
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < cols; ++c) {
if(!grid[r][c] || visit[r][c]) {
visit[r][c] = true;
continue;
}
q.push({r, c});
visit[r][c] = true;
bool border = false;
int count = 0;
while(q.size()) {
auto [row, col] = q.front();
q.pop();
count += 1;
if(!row || !col || row == rows - 1 || col == cols - 1) {
border = true;
}
for(int m = 0; m < 4; ++m) {
int newRow = row + MOVES[m][0];
int newCol = col + MOVES[m][1];
if(newRow < 0 || newCol < 0 || newRow >= rows || newCol >= cols) continue;
if(!grid[newRow][newCol] || visit[newRow][newCol]) continue;
visit[newRow][newCol] = true;
q.push({newRow, newCol});
}
}
if(!border) {
answer += count;
}
}
}
return answer;
}
};
// Accepted
// 58/58 cases passed (84 ms)
// Your runtime beats 39.64 % of cpp submissions
// Your memory usage beats 91.97 % of cpp submissions (27.2 MB)