Skip to content

Commit 8dcbba0

Browse files
committed
LeetCode 999. Available Captures for Rook
1 parent 8e4a9dd commit 8dcbba0

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
294294
| [987. Vertical Order Traversal of a Binary Tree][lc987] | 🔴 Hard | [![python](res/py.png)][lc987py] |
295295
| [990. Satisfiability of Equality Equations][lc990] | 🟠 Medium | [![python](res/py.png)][lc990py] |
296296
| [994. Rotting Oranges][lc994] | 🟠 Medium | [![python](res/py.png)][lc994py] |
297+
| [999. Available Captures for Rook][lc999] | 🟢 Easy | [![python](res/py.png)][lc999py] |
297298
| [1008. Construct Binary Search Tree from Preorder Traversal][lc1008] | 🟠 Medium | [![python](res/py.png)][lc1008py] |
298299
| [1041. Robot Bounded In Circle][lc1041] | 🟠 Medium | [![python](res/py.png)][lc1041py] |
299300
| [1046. Last Stone Weight][lc1046] | 🟢 Easy | [![python](res/py.png)][lc1046py] |
@@ -908,6 +909,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
908909
[lc990py]: leetcode/satisfiability-of-equality-equations.py
909910
[lc994]: https://leetcode.com/problems/rotting-oranges/
910911
[lc994py]: leetcode/rotting-oranges.py
912+
[lc999]: https://leetcode.com/problems/available-captures-for-rook/
913+
[lc999py]: leetcode/available-captures-for-rook.py
911914
[lc1008]: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
912915
[lc1008py]: leetcode/construct-binary-search-tree-from-preorder-traversal.py
913916
[lc1041]: https://leetcode.com/problems/robot-bounded-in-circle/
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)