Approach
Breadth-first search
For Remove Methods From Project, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 43 lines of C++ from the credited upstream file 3310.cpp.
- The implementation visibly relies on sequence storage, work queue.
- 6 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations) {4 vector<int> ans;5 vector<vector<int>> graph(n);6 7 for (const vector<int>& invocation : invocations) {8 const int u = invocation[0];9 const int v = invocation[1];10 graph[u].push_back(v);11 }12 13 queue<int> q{{k}};14 vector<bool> seen(n);15 seen[k] = true;16 17 while (!q.empty())18 for (int sz = q.size(); sz > 0; --sz) {19 const int u = q.front();20 q.pop();21 for (const int v : graph[u])22 if (!seen[v]) {23 q.push(v);24 seen[v] = true;25 }26 }27 28 for (int u = 0; u < n; ++u) {29 if (seen[u])30 continue;31 for (const int v : graph[u])32 if (seen[v]) {33 ans.resize(n);34 iota(ans.begin(), ans.end(), 0);35 return ans;36 }37 ans.push_back(u);38 }39 40 return ans;41 }42};43