-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1222.cpp
More file actions
34 lines (29 loc) · 1001 Bytes
/
Problem-1222.cpp
File metadata and controls
34 lines (29 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Problem - 1222
// https://leetcode.com/problems/queens-that-can-attack-the-king/
// O(n) time complexity and O(1) space complexity
class Solution {
public:
int corX[8] = {1, 1, -1, -1, 0, 0, 1, -1};
int corY[8] = {0, -1, 1, 0, 1, -1, 1, -1};
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
vector <vector <int>> ans;
vector <vector <int>> grid(8, vector <int> (8, 0));
for(int i = 0; i < queens.size(); i++)
grid[queens[i][0]][queens[i][1]] = 1;
for(int i = 0; i < 8; i++) {
int x = king[0];
int y = king[1];
while(1) {
x += corX[i];
y += corY[i];
if(x >= 8 || y >= 8 || x < 0 || y < 0)
break;
if(grid[x][y] == 1) {
ans.push_back({x, y});
break;
}
}
}
return ans;
}
};