Leetcode: Sentence Screen Fitting

Given a rows x cols screen and a sentence represented by a list of words, find how many times the given sentence can be fitted on the screen.

Note:

A word cannot be split into two lines.
The order of words in the sentence must remain unchanged.
Two consecutive words in a line must be separated by a single space.
Total words in the sentence won‘t exceed 100.
Length of each word won‘t exceed 10.
1 ≤ rows, cols ≤ 20,000.
Example 1:

Input:
rows = 2, cols = 8, sentence = ["hello", "world"]

Output:
1

Explanation:
hello---
world---

The character ‘-‘ signifies an empty space on the screen.
Example 2:

Input:
rows = 3, cols = 6, sentence = ["a", "bcd", "e"]

Output:
2

Explanation:
a-bcd-
e-a---
bcd-e-

The character ‘-‘ signifies an empty space on the screen.
Example 3:

Input:
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"]

Output:
1

Explanation:
I-had
apple
pie-I
had--

The character ‘-‘ signifies an empty space on the screen.

先来一个brute force, 类似Text Adjustment

 1 public class Solution {
 2     public int wordsTyping(String[] sentence, int rows, int cols) {
 3         if (sentence==null || sentence.length==0 || sentence.length>rows*cols || rows<=0 || cols<=0)
 4             return 0;
 5         int res = 0;
 6         int j = 0; //indicate the index of string in sentence that is currently trying to be inserted to current row
 7         int row = 0; //current row
 8         int col = 0; //current col
 9
10         while (row < rows) {
11             while (col + sentence[j].length() - 1 < cols) {
12                 col = col + sentence[j].length() + 1;
13                 j++;
14                 if (j == sentence.length) {
15                     res++;
16                     j = 0;
17                 }
18             }
19             row++;
20             col = 0;
21         }
22         return res;
23     }
24 }

但是在稍微大一点的case就TLE了,比如:

["a","b","e"] 20000 20000, 花了465ms

所以想想怎么节约时间,

提示是可以DP的,想想怎么复用,refer to: https://discuss.leetcode.com/topic/62364/java-optimized-solution-17ms

如果这一行由sentence里面某一个string开头,那么,下一行由哪个string开头,这个是确定的;同时,本行会不会到达sentence末尾,如果会,到几次,这个也是一定的

这两点就可以加以利用,因为我们找到了DP的复用关系

sub-problem: if there‘s a new line which is starting with certain index in sentence, what is the starting index of next line (nextIndex[]). BTW, we compute how many times the pointer in current line passes over the last index (times[]).

Time complexity : O(n*(cols/lenAverage)) + O(rows), where n is the length of sentence array, lenAverage is the average length of the words in the input array.

 1 public class Solution {
 2     public int wordsTyping(String[] sentence, int rows, int cols) {
 3         int[] nextInt = new int[sentence.length];
 4         int[] times = new int[sentence.length];
 5         for (int i=0; i<sentence.length; i++) {
 6             int cur = i; //try to insert string with index cur in sentence to current row
 7             int col = 0; //current col
 8             int time = 0;
 9             while (col + sentence[cur].length() - 1 < cols) {
10                 col = col + sentence[cur++].length() + 1;
11                 if (cur == sentence.length) {
12                     cur = 0;
13                     time++;
14                 }
15             }
16             nextInt[i] = cur;
17             times[i] = time;
18         }
19
20         int res = 0;
21         int cur = 0;
22         for (int i=0; i<rows; i++) {
23             res += times[cur];
24             cur = nextInt[cur];
25         }
26         return res;
27     }
28 }
时间: 2024-08-06 03:41:44

Leetcode: Sentence Screen Fitting的相关文章

[LeetCode] Sentence Screen Fitting 调整屏幕上的句子

Given a rows x cols screen and a sentence represented by a list of words, find how many times the given sentence can be fitted on the screen. Note: A word cannot be split into two lines. The order of words in the sentence must remain unchanged. Two c

Sentence Screen Fitting

Given a rows x cols screen and a sentence represented by a list of words, find how many times the given sentence can be fitted on the screen. Note: A word cannot be split into two lines. The order of words in the sentence must remain unchanged. Two c

[LeetCode] Sentence Similarity

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, "great acting skills" and "fine drama talent" are similar, if th

[LeetCode] Sentence Similarity 句子相似度

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, "great acting skills" and "fine drama talent" are similar, if th

[LeetCode] Sentence Similarity II 句子相似度之二

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, words1 = ["great", "acting", "skills"] and words2 = [&

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.