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

 public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols) {
if (sentence==null || sentence.length==0 || sentence.length>rows*cols || rows<=0 || cols<=0)
return 0;
int res = 0;
int j = 0; //indicate the index of string in sentence that is currently trying to be inserted to current row
int row = 0; //current row
int col = 0; //current col while (row < rows) {
while (col + sentence[j].length() - 1 < cols) {
col = col + sentence[j].length() + 1;
j++;
if (j == sentence.length) {
res++;
j = 0;
}
}
row++;
col = 0;
}
return res;
}
}

但是在稍微大一点的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.

 public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols) {
int[] nextInt = new int[sentence.length];
int[] times = new int[sentence.length];
for (int i=0; i<sentence.length; i++) {
int cur = i; //try to insert string with index cur in sentence to current row
int col = 0; //current col
int time = 0;
while (col + sentence[cur].length() - 1 < cols) {
col = col + sentence[cur++].length() + 1;
if (cur == sentence.length) {
cur = 0;
time++;
}
}
nextInt[i] = cur;
times[i] = time;
} int res = 0;
int cur = 0;
for (int i=0; i<rows; i++) {
res += times[cur];
cur = nextInt[cur];
}
return res;
}
}

Leetcode: Sentence Screen Fitting的更多相关文章

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

    Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...

  2. 418. Sentence Screen Fitting

    首先想到的是直接做,然后TLE. public class Solution { public int wordsTyping(String[] sentence, int rows, int col ...

  3. Sentence Screen Fitting

    Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...

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

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. [LeetCode] Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

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

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. HTTP 头字段总结

    1. Accept: 告诉WEB服务器自己接受什么介质类型,/ 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type.2. Accept-Charset: 浏览器申明自己接 ...

  2. three.js材质

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. JBD日志的定位、分析和恢复

    在上一篇中,我们介绍了Ext3文件系统的日志可以看做一个文件,由JBD进行管理.自然而然引出如下这些问题: 1)如何定位ext3日志文件和查看日志文件的裸数据? 2)ext3日志文件数据在物理上是如何 ...

  4. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. mysql错误汇总

    1,mysql报Fatal error encountered during command execution的解决办法 连接字符串里加上 Allow User Variables=True 解决. ...

  6. jQuery基础,定时器,工厂函数

    这个星期刚刚学的JQuery,下面我来说说我学的这几个例子 jQuery是JavaScript的一个程序库. Jquery的工厂函数$(): 在Jquery中 $符号等价于jquery,作用是将DOM ...

  7. Java实现MySQL数据库备份(一)

    下班了,利用闲暇时间总结一下如何使用Java语言实现MySQL数据库备份: import java.io.BufferedReader; import java.io.File; import jav ...

  8. django 有model生成SQL以及现有反向表生成model

    已有models生成SQL语句 语法 python manage.py sqlall app_name   # app_name, 在settings已经导入, 如: INSTALLED_APPS = ...

  9. NotePad++左侧导航

    NotePad++ 1.  增加左侧导航 a. 通过工具栏里面的“插件”->Plugin Manager-> Shwo Plugin Manager b. 找到Explorer勾选,点击I ...

  10. HJ-第二周,真机调试

      报错: Showing Recent Issues   Huiju_app requires a provisioning profile. Select a provisioning profi ...