Solving Leetcode Interviews in Seconds with AI: Available Captures for Rook
Introduction
In this blog post, we will explore how to solve the LeetCode problem "999" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given an 8 x 8 matrix representing a chessboard. There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'. Empty squares are represented by '.'. A rook can move any number of squares horizontally or vertically (up, down, left, right) until it reaches another piece or the edge of the board. A rook is attacking a pawn if it can move to the pawn's square in one move. Note: A rook cannot move through other pieces, such as bishops or pawns. This means a rook cannot attack a pawn if there is another piece blocking the path. Return the number of pawns the white rook is attacking. Example 1: Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] Output: 3 Explanation: In this example, the rook is attacking all the pawns. Example 2: Input: board = [[".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] Output: 0 Explanation: The bishops are blocking the rook from attacking any of the pawns. Example 3: Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]] Output: 3 Explanation: The rook is attacking the pawns at positions b5, d6, and f5. Constraints: board.length == 8 board[i].length == 8 board[i][j] is either 'R', '.', 'B', or 'p' There is exactly one cell with board[i][j] == 'R'
Explanation
Here's a solution to the problem:
- Find the Rook: Locate the position of the white rook 'R' on the chessboard.
- Check in Four Directions: From the rook's position, check for pawns 'p' in each of the four directions (up, down, left, right). Stop checking in a direction when you encounter another piece ('B', 'R', 'p') or the edge of the board.
Count Attacked Pawns: Increment the count of attacked pawns for each pawn found in a clear line of sight from the rook.
Runtime Complexity: O(1), since we iterate at most 8 times in each direction.
- Storage Complexity: O(1).
Code
def numRookCaptures(board):
"""
Calculates the number of pawns the white rook is attacking on the chessboard.
Args:
board: A list of lists of strings representing the chessboard.
Returns:
The number of pawns the white rook is attacking.
"""
rook_row, rook_col = -1, -1
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
rook_row, rook_col = i, j
break
if rook_row != -1:
break
attacked_pawns = 0
# Check upwards
for i in range(rook_row - 1, -1, -1):
if board[i][rook_col] == 'p':
attacked_pawns += 1
break
elif board[i][rook_col] != '.':
break
# Check downwards
for i in range(rook_row + 1, 8):
if board[i][rook_col] == 'p':
attacked_pawns += 1
break
elif board[i][rook_col] != '.':
break
# Check leftwards
for j in range(rook_col - 1, -1, -1):
if board[rook_row][j] == 'p':
attacked_pawns += 1
break
elif board[rook_row][j] != '.':
break
# Check rightwards
for j in range(rook_col + 1, 8):
if board[rook_row][j] == 'p':
attacked_pawns += 1
break
elif board[rook_row][j] != '.':
break
return attacked_pawns