The trick here is to name the state correctly, then let the implementation follow. For 1020. Number of Enclaves, the solution in this repository is mainly a stack solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numEnclaves.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 84 ms, faster than 14.76% of C++ online submissions for Number of Enclaves.
02//Memory Usage: 16.5 MB, less than 62.50% of C++ online submissions for Number of Enclaves.
03class Solution {
04public:
05 int numEnclaves(vector<vector<int>>& A) {
06 int m = A.size(), n = A[0].size();
07
08 vector<vector<bool>> visited(m, vector<bool>(n, 0));
09
10 vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
11
12 stack<vector<int>> stk;
13
14 for(int i = 0; i < m; i++){
15 if(A[i][0] == 1){
16 visited[i][0] = true;
17 stk.push({i, 0});
18 }
19
20 if(A[i][n-1] == 1){
21 visited[i][n-1] = true;
22 stk.push({i, n-1});
23 }
24 }
25
26 for(int j = 1; j < n-1; j++){
27 if(A[0][j] == 1){
28 visited[0][j] = true;
29 stk.push({0, j});
30 }
31
32 if(A[m-1][j] == 1){
33 visited[m-1][j] = true;
34 stk.push({m-1, j});
35 }
36 }
37
38 while(!stk.empty()){
39 vector<int> cur = stk.top(); stk.pop();
40 int curI = cur[0], curJ = cur[1];
41
42 for(vector<int>& dir : dirs){
43 int nextI = curI + dir[0];
44 int nextJ = curJ + dir[1];
45 if(nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && !visited[nextI][nextJ] && A[nextI][nextJ] == 1){
46 visited[nextI][nextJ] = true;
47 stk.push({nextI, nextJ});
48 }
49 }
50 }
51
52 int ans = 0;
53
54 for(int i = 0; i < m; i++){
55 for(int j = 0; j < n; j++){
56 if(A[i][j] == 1 && !visited[i][j]){
57 ans++;
58 }
59 }
60 }
61
62 return ans;
63 }
64};
Cost