Skip to content

Commit 789fabb

Browse files
authored
Create queens-that-can-attack-the-king.py
1 parent ccc36ea commit 789fabb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(1)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def queensAttacktheKing(self, queens, king):
6+
"""
7+
:type queens: List[List[int]]
8+
:type king: List[int]
9+
:rtype: List[List[int]]
10+
"""
11+
dirctions = [(-1, 0), (0, 1), (1, 0), (0, -1),
12+
(-1, 1), (1, 1), (1, -1), (-1, -1)]
13+
result = []
14+
lookup = {(i, j) for i, j in queens}
15+
for dx, dy in dirctions:
16+
for i in xrange(1, 8):
17+
x, y = king[0] + dx*i, king[1] + dy*i
18+
if (x, y) in lookup:
19+
result.append([x, y])
20+
break
21+
return result

0 commit comments

Comments
 (0)