Java O(row + column)


  • 0
    D
    public class Solution {
        public int wordsTyping(String[] sentence, int rows, int cols) {
            if (sentence == null || sentence.length == 0) {
                return 0;
            }
            int[] words = new int[sentence.length];
            for (int i = 0, cursor = 0, num = 0; i < sentence.length; i++) {
                cursor -= i > 0 ? sentence[i - 1].length() + 1 : 0;
                num -= i > 0 ? 1 : 0;
                for (int j = (i + num) % sentence.length; cursor + sentence[j].length() <= cols; j = (j + 1) % sentence.length) {
                    cursor += sentence[j].length() + 1;
                    num++;
                }
                words[i] = num;
            }
            int total = 0;
            for (int row = 0, begin = 0; row < rows; row++) {
                total += words[begin];
                begin = (begin + words[begin]) % sentence.length;
            }
            return total / sentence.length;
        }
    }
    

  • 0
    L
    This post is deleted!

Log in to reply
 

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