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).
Example 1:
Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]] Output: 2
Example 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 board
?
The problem asks to count the number of battleships in a given board represented as a matrix of characters. Battleships are represented by 'X' and empty cells by '.'. Battleships can be placed horizontally or vertically, but never diagonally, and they cannot touch each other (horizontally or vertically).
The most efficient approach is a single pass through the board, avoiding unnecessary recursion or complex data structures. The core idea is to identify only the top-leftmost cell of each battleship.
Algorithm:
ans
to 0. This will store the count of battleships.ans
.ans
.Time Complexity: O(m*n), where 'm' and 'n' are the dimensions of the board. We visit each cell exactly once.
Space Complexity: O(1). We use a constant amount of extra space to store the counter ans
. No additional data structures are needed.
Code Implementation (Python):
class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
m, n = len(board), len(board[0])
ans = 0
for i in range(m):
for j in range(n):
if board[i][j] == 'X': #Check if it's a battleship cell
if (i > 0 and board[i - 1][j] == 'X') or \
(j > 0 and board[i][j - 1] == 'X'):
continue #Skip if part of an already counted battleship
ans += 1 #Increment count for a new battleship
return ans
Code Implementation (Java):
class Solution {
public int countBattleships(char[][] board) {
int m = board.length;
int n = board[0].length;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'X') {
if ((i > 0 && board[i - 1][j] == 'X') ||
(j > 0 && board[i][j - 1] == 'X')) {
continue;
}
ans++;
}
}
}
return ans;
}
}
Code Implementation (JavaScript):
/**
* @param {character[][]} board
* @return {number}
*/
var countBattleships = function(board) {
let count = 0;
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
if (board[i][j] === 'X') {
if ((i > 0 && board[i-1][j] === 'X') || (j > 0 && board[i][j-1] === 'X')) continue;
count++;
}
}
}
return count;
};
The code in other languages (C++, Go, TypeScript) would follow a very similar structure, adapting syntax as needed. The core logic remains the same.