dimanche 9 avril 2017

how can i debug in online judge when the input is huge?

Today i have spent several hour on debug a program in online judge-- the problem LeetCode

my code is: it pass the 60/66 test case, I don't know where the mistake is.I hope you can give me a good answer about my mistake and how i debug when the input is huge.

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix==null||matrix.length==0) return 0;
        short[][] smatrix=new short[matrix.length+1][];
        smatrix[matrix.length]=new short[matrix[0].length];
        for(int i=0;i<matrix.length;i++){
            smatrix[i]=new short[matrix[i].length];
            for(int j=0;j<matrix[i].length;j++)
                smatrix[i][j]=(short) (matrix[i][j]-'0');
        }
        for(int j=0;j<matrix[0].length;j++)
            smatrix[matrix.length][j]=0;

        //travel ever row 
        int max=-1;
        for(int i=0;i<smatrix.length;i++){
            count(smatrix[i]);
            for(int j=0;j<smatrix[i].length;j++)
                max=smatrix[i][j]>max?smatrix[i][j]:max;
        }
        if(smatrix.length==1) return max;


        //travel every row and each row to the tail-1
       for(int i=0;i<smatrix.length-1;i++){
            andTwoRows(smatrix[i],smatrix[i+1]);
            for(int j=i+1;j<smatrix.length;j++){
                max=andTwoRows(max,j-i,smatrix[i],smatrix[j]);
                if(!haveOne(smatrix[i])) break;
            }
       }
       return max;
    }
    private void andTwoRows(short[] head,short[] cur){
        short[] onehead=new short[head.length];
        short[] onecur=new short[cur.length];
        for(int i=0;i<head.length;i++){
            if(head[i]>0)
                for(int j=0;j<head[i];j++) onehead[i+j]=1;
            if(cur[i]>0)
                for(int j=0;j<cur[i];j++) onecur[i+j]=1;
        }
        for(int i=0;i<head.length;i++)
            head[i]=(short) (onehead[i]&onecur[i]);
        count(head);
    }

    private void count(short[] rows){
        for(int i=0,k=-1;i<rows.length;i++){
            if(rows[i]==0) k=-1;
            if(k<0&&rows[i]==1) {k=i;continue;}
            if(k>=0) {rows[k]+=rows[i];rows[i]=0;}
        }
    }
    private int andTwoRows(int max,int h,short[] head,short[] cur){
        short[] res=new short[head.length];
        System.arraycopy(head,0,res,0,head.length);
        andTwoRows(res,cur);

        for(int i=0;i<head.length;i++){
            if(res[i]<head[i]){
                max=h*head[i]>max?h*head[i]:max;
                head[i]=res[i];
            }
        }
        return max;
    }
    private boolean haveOne(short[] row){
        for(short tmp:row)
            if(tmp>0) return true;
        return false;
    }
}

the test case : test case

since the test case input is huge. how should i do when i want to debug.

Aucun commentaire:

Enregistrer un commentaire