test case wrong?


  • 0

    I run the same testcase in my IDE, the result is 1. Why here is 2.

    Input:
    ["W","E","W","0","E"]
    Output:
    2
    Expected:
    1
    
    
    public int maxKilledEnemies(char[][] grid) {
    
        int m = grid.length;
        if(m == 0) return 0;
        int n = grid[0].length;
        int rowKill=0, res = 0;
        int[] colKill = new int[n];
        for(int i=0;i<m;i++){
    
            for(int j=0;j<n;j++){
                if(j==0 || grid[i][j-1] == 'W'){
                    rowKill = 0;
                    for(int k=j;k<n && grid[i][k]!='W';k++){
                        rowKill += grid[i][k] == 'E'?1:0;
                    }
                }
                if(i==0 || grid[i-1][j] == 'W'){
                    for(int k=i;k<m && grid[k][j]!='W';k++){
                        colKill[j] += grid[k][j] == 'E'?1:0;
                    }
                }
                if(grid[i][j] == '0'){
                    res = Math.max(res, rowKill + colKill[j]);
                }
            }
        }
    
        return res;
    }

  • 0

    @hot13399 said in test case wrong?:

    I run the same testcase in my IDE, the result is 1.

    Then you didn't really use that code and that test case. If you did, the result would be 2 in your IDE as well.


  • 0

    Thank you @StefanPochmann , yes, I misunderstood the testcase, and there is a bug in my code.
    ["W","E","W","0","E"] actually means:

    char[][] grid= new char[][]{{'W'},{'E'},{'W'},{'0'},{'E'}};
    

    instead of:

    char[][] grid= new char[][]{{'W','E','W','0','E'}};
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.