-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0419 [M] Battleships in a Board
Code with Senpai edited this page Feb 23, 2022
·
1 revision
class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
n_rows = len(board)
n_cols = len(board[0])
count = 0
for r in range(n_rows):
for c in range(n_cols):
if board[r][c] != 'X': continue
# if current cell is X, then there can be no adjacent
if r > 0 and board[r-1][c] == 'X': continue
if c > 0 and board[r][c-1] == 'X': continue
count += 1
return count
footer