Battleships in a Board - Complete Solution Guide
Battleships in a Board is LeetCode problem 419, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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"],[".","
Detailed Explanation
The problem asks us to count the number of battleships present in a given 2D board. The board contains 'X' representing a battleship and '.' representing an empty cell. Battleships are placed either horizontally or vertically, and two battleships are always separated by at least one horizontal or vertical cell. The goal is to efficiently count the number of distinct battleships without overcounting connected parts of the same ship.
Solution Approach
The provided solution iterates through each cell of the board. If a cell contains a battleship ('X'), it checks if it is the starting point (head) of the battleship. A cell is considered the starting point if the cell above it and the cell to its left are not part of another battleship (i.e., they are not 'X'). If it's a starting point, the battleship count is incremented.
Step-by-Step Algorithm
- Step 1: Initialize a count variable to 0. This variable will store the number of battleships.
- Step 2: Iterate through each cell in the board, row by row.
- Step 3: For each cell, check if the cell contains 'X'.
- Step 4: If the cell contains 'X', check if it's the starting point of a battleship. To do this, check if the cell above and the cell to the left are also 'X'. If either of them is 'X', then this cell is a part of a battleship that has already been counted, so we skip counting it again.
- Step 5: If the current cell is 'X' and it's the starting point of a battleship (i.e., no 'X' above or to the left), increment the count.
- Step 6: After iterating through the entire board, return the final count of battleships.
Key Insights
- Insight 1: A battleship's starting point is the top-left cell if it is placed vertically or horizontally. This is because no two battleships are adjacent.
- Insight 2: We can identify the 'head' of a battleship by checking if the cell is an 'X' and if the cell above and to the left are not 'X's. This prevents overcounting segments of the same battleship.
- Insight 3: The problem can be solved in one pass and O(1) space, without modifying the input board, as hinted by the follow-up question.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Depth-First Search, Matrix.
Companies
Asked at: Microstrategy, Tinkoff.