> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Battleships in a Board Python Solution

> Tested Python solution for LeetCode 419 with 34 pytest cases. Generate a practice environment with lcpy.

LeetCode 419, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Depth-First Search](/catalog/topics/depth-first-search), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/battleships-in-a-board/description/).

Generate this problem as a practice environment: tested reference solution, 34 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 419   # by problem number
lcpy gen -s battleships_in_a_board   # by problem name
```

## Problem

Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return the number of the battleships on `board`.

Battleships can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, `1` column), where `k` can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/06/21/image.png)

```
Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2
```

```
Input: board = [["."]]
Output: 0
```

### Constraints

* m == board.length
* n == board\[i].length
* 1 \<= m, n \<= 200
* board\[i]\[j] is either '.' or 'X'.

Follow up: Could you do it in one-pass, using only O(1) extra memory and without modifying the values of board?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/battleships_in_a_board/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/battleships_in_a_board/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(1)
    def count_battleships(self, board: list[list[str]]) -> int:
        count = 0
        for r, row in enumerate(board):
            for c, cell in enumerate(row):
                if cell != "X":
                    continue
                if r > 0 and board[r - 1][c] == "X":
                    continue
                if c > 0 and row[c - 1] == "X":
                    continue
                count += 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.