Header Ad

Leetcode Battleships in a Board problem solution

In this Leetcode Battleships in a Board problem solution we have 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).

Leetcode Battleships in a Board problem solution


Problem solution in Python.

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        ships = 0
        for i, row in enumerate(board):
            for j, slot in enumerate(row):
                if slot == 'X':
                    if i >= 1 and board[i-1][j] == "X":
                        continue
                    if j >= 1 and board[i][j-1] == "X":
                        continue
                    ships += 1
        return ships



Problem solution in Java.

class Solution {
    public int countBattleships(char[][] board) {
        int ans=0;
        for(int i=0;i<board.length;i++)
        {
            for(int j=0;j<board[0].length;j++)
            {
                if(board[i][j] == 'X')
                {
                    if(i == 0&& j == 0)
                      ans++;
                    else if(i==0)
                    {
                        if(board[i][j-1]!='X')
                            ans++;
                    }
                    else if(j==0)
                    {
                        if(board[i-1][j] != 'X')
                            ans++;
                    }
                    else{
                        if(board[i-1][j] != 'X' && board[i][j-1]!='X')
                            ans++;
                    }
                }
            }
        }
        return ans;
    }
}


Problem solution in C++.

int countBattleships(vector<vector<char>>& board) {
    
    
    int rows=board.size();
    int cols=board[0].size();
    
    int ans=0;
    for(int i=0; i<rows; i++){
        
        for(int j=0; j<cols; j++){
            if(i==0 && j==0){
                if(board[i][j]!='X')
                    continue;
            }
            else if(i==0){
                if(board[i][j-1]=='X')
                    continue;
            }
            else if(j==0){
                if(board[i-1][j]=='X')
                    continue;
            }
            else{
                if(board[i-1][j] =='X' || board[i][j-1]=='X')
                    continue;
            }
            if(board[i][j]=='X')
                ans++;
        }
    }
    return ans;
}


Problem solution in C.

int countBattleships(char** board, int boardSize, int* boardColSize){
    int x,y;
    int cnt = 0;
 
    for(y = 0; y<boardSize; y++) {
        x = 0;
        while(x<boardColSize[y]) {
            while(x<boardColSize[y] && board[y][x] == '.') x++;
            // Found
            if(x == boardColSize[y])
                break;
            if (y == 0) {
                // First line. Just increase the count
                cnt ++;
                while(x<boardColSize[y] && board[y][x] == 'X') x++;
            } else {
                if(board[y-1][x] != 'X') {
                    // New ship
                    cnt++;
                }
                while(x<boardColSize[y] && board[y][x] == 'X') x++;
            }
        }
    }
    return cnt;
}


Post a Comment

0 Comments