The returned result from submission is not correct


  • 0
    Q

    Here is my code, for the case rows = 3, cols = 6, sentence = ["a", "bcd", "e"], when I run it from an IDE, it returns correct answer 2, but when I submit, it returns 3. please fix it.

    static int count = 0;
    public int wordsTyping(String[] sentence, int nRow, int nCol) {
        int row = 0, col = -1;
        if(col > nCol) return 0;
    
        while(row < nRow && row != -1){
            int[] loc = itrSentence(sentence, nRow, nCol ,row, col);
            row = loc[0];
            col = loc[1];
        }
        return count;
    }
    public int[] itrSentence(String[] sentence, int nRow, int nCol, int row, int col){
        for(int i = 0 ; i < sentence.length; i++){
            String word = sentence[i];
            if(word.length() > nCol) return new int[]{-1, -1};
            if(word.length()+1 > nCol - col){
                row++; col = 0;
                if(row > nRow - 1) return new int[]{row, col};
                else col = word.length()+1;
            }
            else col += word.length()+1;
        }
        count++;
        return new int[]{row, col};
    }
    
    

Log in to reply
 

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