Intuition
- The brute force method of checking up down right left starting from the rook positions seems to be the best solution here.
Complexity
Runtime
to initially find the position of the rook.
Space
Code
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
pawn_positions = []
bishop_positions = []
rook_r, rook_c = -1, -1
broken = True
for r in range(8):
for c in range(8):
if board[r][c] == 'R':
rook_r, rook_c = r, c
res = 0
i = rook_r - 1
# Go up from rook
while i >= 0:
if board[i][rook_c] == 'B':
break
elif board[i][rook_c] == 'p':
res += 1
break
i -= 1
# Go down from rook
i = rook_r + 1
while i < 8:
if board[i][rook_c] == 'B':
break
elif board[i][rook_c] == 'p':
res += 1
break
i += 1
# Go left from rook
i = rook_c - 1
while i >= 0:
if board[rook_r][i] == 'B':
break
elif board[rook_r][i] == 'p':
res += 1
break
i -= 1
# Go right from rook
i = rook_c + 1
while i < 8:
if board[rook_r][i] == 'B':
break
elif board[rook_r][i] == 'p':
res += 1
break
i += 1
return res