|
| 1 | +--- |
| 2 | +id: battleships-in-a-board |
| 3 | +title: Battleships in a Board |
| 4 | +sidebar_label: 0419-Battleships-in-a-Board |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Depth-First Search |
| 8 | +- Matrix |
| 9 | +description: "Given an `m x n` board where each cell is a battleship 'X' or empty '.', count the number of battleships on the board." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem |
| 13 | + |
| 14 | +Given an `m x n` board where each cell is a battleship `'X'` or empty `'.'`, return the number of battleships on the board. |
| 15 | + |
| 16 | +Battleships can only be placed horizontally or vertically on the board. In other words, they can only occupy a contiguous line of cells horizontally or vertically. Each battleship must be separated by at least one empty cell (either horizontally or vertically) from any other battleship. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +**Input:** `board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]` |
| 23 | +**Output:** `2` |
| 24 | + |
| 25 | +**Example 2:** |
| 26 | + |
| 27 | +**Input:** `board = [["."]]` |
| 28 | +**Output:** `0` |
| 29 | + |
| 30 | +### Constraints |
| 31 | + |
| 32 | +- `m == board.length` |
| 33 | +- `n == board[i].length` |
| 34 | +- `1 <= m, n <= 200` |
| 35 | +- `board[i][j]` is either `'X'` or `'.'`. |
| 36 | + |
| 37 | +### Follow-up |
| 38 | + |
| 39 | +Could you do it in one-pass, using only `O(1)` extra memory and without modifying the value of the board? |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Approach |
| 44 | + |
| 45 | +To count the number of battleships in one pass and without extra memory, we can traverse the board and count only the top-left cell of each battleship. A cell qualifies as the top-left cell if there are no battleship cells above it or to the left of it. |
| 46 | + |
| 47 | +### Steps: |
| 48 | + |
| 49 | +1. Initialize a counter to keep track of the number of battleships. |
| 50 | +2. Traverse each cell in the board. |
| 51 | +3. For each cell, check if it is a battleship (`'X'`): |
| 52 | + - If it is, check if there is no battleship cell above it and no battleship cell to the left of it. |
| 53 | + - If both conditions are met, increment the battleship counter. |
| 54 | +4. Return the counter after the traversal. |
| 55 | + |
| 56 | +### Solution |
| 57 | + |
| 58 | +#### Java Solution |
| 59 | + |
| 60 | +```java |
| 61 | +class Solution { |
| 62 | + public int countBattleships(char[][] board) { |
| 63 | + int count = 0; |
| 64 | + for (int i = 0; i < board.length; i++) { |
| 65 | + for (int j = 0; j < board[0].length; j++) { |
| 66 | + if (board[i][j] == 'X') { |
| 67 | + if (i > 0 && board[i - 1][j] == 'X') continue; |
| 68 | + if (j > 0 && board[i][j - 1] == 'X') continue; |
| 69 | + count++; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return count; |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | +### C++ Solution |
| 78 | + |
| 79 | +```cpp |
| 80 | +class Solution { |
| 81 | +public: |
| 82 | + int countBattleships(vector<vector<char>>& board) { |
| 83 | + int count = 0; |
| 84 | + for (int i = 0; i < board.size(); i++) { |
| 85 | + for (int j = 0; j < board[0].size(); j++) { |
| 86 | + if (board[i][j] == 'X') { |
| 87 | + if (i > 0 && board[i - 1][j] == 'X') continue; |
| 88 | + if (j > 0 && board[i][j - 1] == 'X') continue; |
| 89 | + count++; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return count; |
| 94 | + } |
| 95 | +}; |
| 96 | +``` |
| 97 | +### Python Solution |
| 98 | +
|
| 99 | +```python |
| 100 | +class Solution: |
| 101 | + def countBattleships(self, board: List[List[str]]) -> int: |
| 102 | + count = 0 |
| 103 | + for i in range(len(board)): |
| 104 | + for j in range(len(board[0])): |
| 105 | + if board[i][j] == 'X': |
| 106 | + if i > 0 and board[i - 1][j] == 'X': |
| 107 | + continue |
| 108 | + if j > 0 and board[i][j - 1] == 'X': |
| 109 | + continue |
| 110 | + count += 1 |
| 111 | + return count |
| 112 | +``` |
| 113 | +### Complexity Analysis |
| 114 | +**Time Complexity:** O(m * n) |
| 115 | +>Reason: We traverse each cell of the board once. |
| 116 | + |
| 117 | +**Space Complexity:** O(1) |
| 118 | +>Reason: We do not use any extra space that scales with the input size. |
| 119 | +
|
| 120 | +### References |
| 121 | +**LeetCode Problem:** Battleships in a Board |
0 commit comments