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
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的更多相关文章
- [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 ...
- 418. Sentence Screen Fitting
首先想到的是直接做,然后TLE. public class Solution { public int wordsTyping(String[] sentence, int rows, int col ...
- Sentence Screen Fitting
Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 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 ...
随机推荐
- tcp三次握手和四次握手
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- 2016 Multi-University Training Contest 1 F.PowMod
PowMod Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- uva10986 堆优化单源最短路径(pas)
var n,m,s,t,v,i,a,b,c:longint;//这道题的代码不是这个,在下面 first,tr,p,q:..]of longint; next,eb,ew:..]of longint; ...
- 对称、非对称加密算,openssl生成证书(笔记)
对称加密算法 1.密钥只有一个,加密和解密都需要同一个密钥2.DES,IDEA,AES3.明文+密钥=密文, 密文+密钥=明文4.加密速度快,系统开销小,适用大量数据的加密 非对称加密算法1.密钥由公 ...
- 一、jquery简介
认识jquery jquery是有美国人John Resig于2006年创建的一个开元项目,随着被人们的熟知,越来越多的程序高手加入其中,完善和壮大其项目内容:如今已开展成为集javascript.c ...
- hibernate关联关系笔记
Hibernate关联关系笔记 单向N:1 * 有连接表:在N方使用<join>/<many-to-one>.1方无需配置与之关联的持久化类. * 没有连接表:在N方使用& ...
- Java备份Oracle数据库
Java备份Oracle数据库 Java线程.Process.ProcessBuilder 2010 年 6 月 20 日 文章内容描述了使用Java执行外部Oracle导出命令备份数据库功能的示例, ...
- Apache Spark技术实战之9 -- 日志级别修改
摘要 在学习使用Spark的过程中,总是想对内部运行过程作深入的了解,其中DEBUG和TRACE级别的日志可以为我们提供详细和有用的信息,那么如何进行合理设置呢,不复杂但也绝不是将一个INFO换为TR ...
- Yii源码阅读笔记(三十五)
Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...
- 掌握Thinkphp3.2.0----内置标签
使用内置标签的时候,一定要注意闭合-----单标签自闭合,双标签对应闭合 标签的学习在于记忆和应用 一. 判断比较 //IF 语句的完整格式 <if condition="$user ...