Took my Japanese class today, it was fun. But I’m so tired now, just did one medium Google problem, with two solutions. Not a bad day …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class Solution { public int wordsTyping(String[] sentence, int rows, int cols) { String s = String.join(" ", sentence) + " "; int start = 0, l = s.length(); for (int i = 0; i < rows; i++) { start += cols; if (s.charAt(start % l) == ' ') { start++; } else { while (start > 0 && s.charAt((start-1) % l) != ' ') { start--; } } } return start / s.length(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class Solution { public int wordsTyping(String[] sentence, int rows, int cols) { int[] dp = new int[sentence.length]; int n = sentence.length; for(int i = 0, prev = 0, len = 0; i < sentence.length; ++i) { if(i != 0 && len > 0) len -= sentence[i - 1].length() + 1; while(len + sentence[prev % n].length() <= cols) len += sentence[prev++ % n].length() + 1; dp[i] = prev; } int count = 0; for(int i = 0, k = 0; i < rows; ++i) { count += dp[k] - k; k = dp[k] % n; } return count / n; } }
|
近期评论