Share my Java codes


  • 0
    C

    Share my codes. I didnot do too much search here. so, if repeated, tell me, I will delete my post.

    public class Solution {
        public int maxKilledEnemies(char[][] grid) {
            if(grid==null||grid.length==0||grid[0].length==0) return 0;
            int ROW = grid.length, COL = grid[0].length;
            int[][] dp1 = new int[ROW][COL], dp2 = new int[ROW][COL];
            for(int row = 0; row < ROW; row++) {
                int start = 0, count = 0;
                for(int col = 0; col <= COL; col++) {
                    if( col==COL || grid[row][col]=='W' ) {
                        for(int i = start; i < col; i++) if(grid[row][i]=='0') dp1[row][i] = count;
                        count = 0;
                        start = col+1;
                    } else if( grid[row][col]=='E' ) count++;
                }
            }
            
            for(int col = 0; col < COL; col++) {
                int start = 0, count = 0;
                for(int row = 0; row <= ROW; row++) {
                    if( row==ROW || grid[row][col]=='W' ) {
                        for(int i = start; i < row; i++) if(grid[i][col]=='0') dp2[i][col] = count;
                        count = 0;
                        start = row+1;
                    } else if( grid[row][col]=='E' ) count++;
                }
            }
    
            int max = 0;
            for(int row = 0; row < ROW; row++) {
                for(int col = 0; col < COL;col++) {
                    max = Math.max(max, dp1[row][col]+dp2[row][col]);
                }
            }
            return max;
        }
    }

Log in to reply
 

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