Leetcode - Bomb Enemy

My code:

public class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        
        int max = 0;
        int row = 0;
        int[] cols = new int[grid[0].length];
        
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (j == 0 || grid[i][j - 1] == 'W') {
                    row = updateRow(i, j, grid);
                }
                if (i == 0 || grid[i - 1][j] == 'W') {
                    cols[j] = updateCol(i, j, grid);
                }
                if (grid[i][j] == '0') {
                    max = Math.max(max, row + cols[j]);
                }
            }
        }
        return max;
    }
    
    private int updateRow(int i, int j, char[][] grid) {
        int counter = 0;
        for (int k = j; k < grid[0].length; k++) {
            if (grid[i][k] == 'W') {
                break;
            }
            else if (grid[i][k] == 'E') {
                counter++;
            }
        }
        return counter;
    }
    
    private int updateCol(int i, int j, char[][] grid) {
        int counter = 0;
        for (int k = i; k < grid.length; k++) {
            if (grid[k][j] == 'W') {
                break;
            }
            else if (grid[k][j] == 'E') {
                counter++;
            }
        }
        return counter;
    }
}

reference:
https://discuss.leetcode.com/topic/48742/simple-dp-solution-in-java/2

这道题目我自己也做了出来。
首先是 brute-force,
然后思考,哪里可以加cache
发现,从列角度看,如果左侧是'0',那么,当前这个点,这一行的炸死敌人数,就等于左边这一点,这一行的数。
同理,从行角度看,如果上侧是'0',那么,当前这个点,这一列的炸死敌人数,就等于上边这一点,这一列的数。
用这个原理加cache
因为每个点都有 行炸死个数,列炸死个数。
我搞了两个二维数组作为cache
int[][] rows
int[][] cols

其实原理和上面的解法原理是相同的。只是cache规模太大,浪费了很多时间。
我的解法如下:

My code:

public class Solution {
    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        
        int max = 0;
        int[][] cols = new int[grid.length][grid[0].length];
        int[][] rows = new int[grid.length][grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '0') {
                    max = Math.max(max, place(i, j, grid, rows, cols));
                }
            }
        }
        return max;
    }
    
    private int place(int i, int j, char[][] grid, int[][] rows, int[][] cols) {
        int row = 0;
        int col = 0;
        boolean colHit = false;
        boolean rowHit = false;
        if (i > 0 && grid[i - 1][j] == '0') {
            col += cols[i - 1][j];
            colHit = true;
        }
        if (j > 0 && grid[i][j - 1] == '0') {
            row += rows[i][j - 1];
            rowHit = true;
        }
        
        if (!colHit) {
            // up
            for (int k = i - 1; k >= 0; k--) {
                if (grid[k][j] == 'W') {
                    break;
                }
                else if (grid[k][j] == 'E') {
                    col++;
                }
            }
            // down
            for (int k = i + 1; k < grid.length; k++) {
                if (grid[k][j] == 'W') {
                    break;
                }
                else if (grid[k][j] == 'E') {
                    col++;
                }
            }
        }
        if (!rowHit) {
            // left
            for (int k = j - 1; k >= 0; k--) {
                if (grid[i][k] == 'W') {
                    break;
                }
                else if (grid[i][k] == 'E') {
                    row++;
                }
            }
            // right
            for (int k = j + 1; k < grid[0].length; k++) {
                if (grid[i][k] == 'W') {
                    break;
                }
                else if (grid[i][k] == 'E') {
                    row++;
                }
            }
        }
        
        cols[i][j] = col;
        rows[i][j] = row;
        return col + row;
    }
}

Anyway, Good luck, Richardo! -- 09/20/2016

你可能感兴趣的:(Leetcode - Bomb Enemy)