Java easy way beats 99%


  • 0
    T
    public int longestLine(int[][] M) {
            if(M.length == 0 || M[0].length == 0) return 0;
            int m = M.length, n = M[0].length;
            int[] col = new int[n];
            int[] dia = new int[n + m];
            int[] antidia = new int[n + m];
            int ret = 0;
            for(int i = 0; i < m; i++){
                int cur = 0;
                for(int j = 0; j < n; j++){
                    if(M[i][j] == 0){
                        cur = 0;
                        col[j] = 0;
                        dia[m - i + j] = 0;
                        antidia[i + j] = 0;
                    }else{
                        cur ++;
                        col[j] ++;
                        dia[m - i + j] ++;
                        antidia[i + j] ++;
                        ret = Math.max(ret, cur);
                        ret = Math.max(ret, col[j]);
                        ret = Math.max(ret, dia[m - i + j]);
                        ret = Math.max(ret, antidia[i + j]);
                    }
                }
            }
            return ret;
        }
    

Log in to reply
 

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