|
| 1 | +# 999. Available Captures for Rook |
| 2 | +# 🟢 Easy |
| 3 | +# |
| 4 | +# https://leetcode.com/problems/available-captures-for-rook/ |
| 5 | +# |
| 6 | +# Tags: Array - Matrix - Simulation |
| 7 | + |
| 8 | +import timeit |
| 9 | +from typing import List |
| 10 | + |
| 11 | + |
| 12 | +# Iterate over the matrix rows and columns to find the rook, then travel |
| 13 | +# on the four directions of movement from the rook's position until we |
| 14 | +# either run out of cells to visit, find a bishop, or find a pawn, if |
| 15 | +# the latter, add one to the result. |
| 16 | +# |
| 17 | +# Time complexity: O(1) - We visit all cells, limited to 64, constant |
| 18 | +# time O(64) => O(1). |
| 19 | +# Space complexity: O(1) - Constant space. |
| 20 | +# |
| 21 | +# Runtime: 30 ms, faster than 96.32% |
| 22 | +# Memory Usage: 13.9 MB, less than 81.84% |
| 23 | +class Solution: |
| 24 | + def numRookCaptures(self, board: List[List[str]]) -> int: |
| 25 | + # Travel through the board to find the rook. |
| 26 | + for i in range(len(board)): |
| 27 | + for j in range(len(board[0])): |
| 28 | + if board[i][j] == "R": |
| 29 | + break |
| 30 | + else: |
| 31 | + continue |
| 32 | + break |
| 33 | + res, row, col = 0, i, j |
| 34 | + # Up. |
| 35 | + i = row - 1 |
| 36 | + while i >= 0 and board[i][col] != "B": |
| 37 | + if board[i][col] == "p": |
| 38 | + res += 1 |
| 39 | + break |
| 40 | + i -= 1 |
| 41 | + i = row + 1 |
| 42 | + while i < len(board) and board[i][col] != "B": |
| 43 | + if board[i][col] == "p": |
| 44 | + res += 1 |
| 45 | + break |
| 46 | + i += 1 |
| 47 | + j = col - 1 |
| 48 | + while j >= 0 and board[row][j] != "B": |
| 49 | + if board[row][j] == "p": |
| 50 | + res += 1 |
| 51 | + break |
| 52 | + j -= 1 |
| 53 | + j = col + 1 |
| 54 | + while j < len(board[0]) and board[row][j] != "B": |
| 55 | + if board[row][j] == "p": |
| 56 | + res += 1 |
| 57 | + break |
| 58 | + j += 1 |
| 59 | + return res |
| 60 | + |
| 61 | + |
| 62 | +def test(): |
| 63 | + executors = [ |
| 64 | + Solution, |
| 65 | + ] |
| 66 | + tests = [ |
| 67 | + [ |
| 68 | + [ |
| 69 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 70 | + [".", ".", ".", "p", ".", ".", ".", "."], |
| 71 | + [".", ".", ".", "R", ".", ".", ".", "p"], |
| 72 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 73 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 74 | + [".", ".", ".", "p", ".", ".", ".", "."], |
| 75 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 76 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 77 | + ], |
| 78 | + 3, |
| 79 | + ], |
| 80 | + [ |
| 81 | + [ |
| 82 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 83 | + [".", "p", "p", "p", "p", "p", ".", "."], |
| 84 | + [".", "p", "p", "B", "p", "p", ".", "."], |
| 85 | + [".", "p", "B", "R", "B", "p", ".", "."], |
| 86 | + [".", "p", "p", "B", "p", "p", ".", "."], |
| 87 | + [".", "p", "p", "p", "p", "p", ".", "."], |
| 88 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 89 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 90 | + ], |
| 91 | + 0, |
| 92 | + ], |
| 93 | + [ |
| 94 | + [ |
| 95 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 96 | + [".", ".", ".", "p", ".", ".", ".", "."], |
| 97 | + [".", ".", ".", "p", ".", ".", ".", "."], |
| 98 | + ["p", "p", ".", "R", ".", "p", "B", "."], |
| 99 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 100 | + [".", ".", ".", "B", ".", ".", ".", "."], |
| 101 | + [".", ".", ".", "p", ".", ".", ".", "."], |
| 102 | + [".", ".", ".", ".", ".", ".", ".", "."], |
| 103 | + ], |
| 104 | + 3, |
| 105 | + ], |
| 106 | + ] |
| 107 | + for executor in executors: |
| 108 | + start = timeit.default_timer() |
| 109 | + for _ in range(1): |
| 110 | + for col, t in enumerate(tests): |
| 111 | + sol = executor() |
| 112 | + result = sol.numRookCaptures(t[0]) |
| 113 | + exp = t[1] |
| 114 | + assert result == exp, ( |
| 115 | + f"\033[93m» {result} <> {exp}\033[91m for" |
| 116 | + + f" test {col} using \033[1m{executor.__name__}" |
| 117 | + ) |
| 118 | + stop = timeit.default_timer() |
| 119 | + used = str(round(stop - start, 5)) |
| 120 | + cols = "{0:20}{1:10}{2:10}" |
| 121 | + res = cols.format(executor.__name__, used, "seconds") |
| 122 | + print(f"\033[92m» {res}\033[0m") |
| 123 | + |
| 124 | + |
| 125 | +test() |
0 commit comments