This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1222. Queens That Can Attack the King, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are inQueens.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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: 4 ms, faster than 91.87% of C++ online submissions for Queens That Can Attack the King.
02//Memory Usage: 9.8 MB, less than 100.00% of C++ online submissions for Queens That Can Attack the King.
03
04class Solution {
05public:
06 vector<vector<int>> queens;
07 vector<vector<int>> ans;
08
09 bool inQueens(vector<int>& cur){
10 bool found = false;
11 if(find(queens.begin(), queens.end(), cur) != queens.end()){
12 ans.push_back(cur);
13 found = true;
14 }
15 return found;
16 }
17
18 vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
19 vector<int> cur;
20 this->queens = queens;
21
22 for(int i = king[0]; i >= 0; i--){
23 cur = {i, king[1]};
24 if(inQueens(cur)) break;
25 }
26
27 for(int i = king[0]; i < 8; i++){
28 cur = {i, king[1]};
29 if(inQueens(cur)) break;
30 }
31
32 for(int j = king[1]; j >= 0; j--){
33 cur = {king[0], j};
34 if(inQueens(cur)) break;
35 }
36
37 for(int j = king[1]; j < 8; j++){
38 cur = {king[0], j};
39 if(inQueens(cur)) break;
40 }
41
42 for(int i = king[0], j = king[1]; i >= 0 && j >= 0; i--, j--){
43 cur = {i, j};
44 if(inQueens(cur)) break;
45 }
46
47 for(int i = king[0], j = king[1]; i < 8 && j < 8; i++, j++){
48 cur = {i, j};
49 if(inQueens(cur)) break;
50 }
51
52 for(int i = king[0], j = king[1]; i < 8 && j >= 0; i++, j--){
53 cur = {i, j};
54 if(inQueens(cur)) break;
55 }
56
57 for(int i = king[0], j = king[1]; i >= 0 && j < 8; i--, j++){
58 cur = {i, j};
59 if(inQueens(cur)) break;
60 }
61
62 return ans;
63 }
64};
Cost