Skip to content

Latest commit

 

History

History
43 lines (38 loc) · 1.04 KB

Question_418.md

File metadata and controls

43 lines (38 loc) · 1.04 KB

LeetCode Records - Question 418 Sentence Screen Fitting

Attempt 1: Use an int[] to store the length of each string

class Solution {
    public int wordsTyping(String[] sentence, int rows, int cols) {
        int[] lens = new int[sentence.length];
        for (int i = 0; i < sentence.length; i++) {
            int len = sentence[i].length();
            if (len > cols) {
                return 0;
            }
            lens[i] = len;
        }

        int count = 0;
        int i = 0;
        int j = 0;
        int lenIndex = 0;
        while (i < rows) {
            int newJ = j + lens[lenIndex];
            if (newJ <= cols) {
                j = newJ + 1;
                lenIndex++;

                if (lenIndex == sentence.length) {
                    lenIndex = 0;
                    count++;
                }
            } else {
                i++;
                j = 0;
            }
        }

        return count;
    }
}
  • Runtime: 1337 ms (Beats: 51.19%)
  • Memory: 41.02 MB (Beats: 33.08%)