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. cookie编码乱码问题与cookie禁用后session操作

    Cookie传输的值只能是ASCII码,该编码表相对老旧不含有很多符号与文字 特别是中文,所以在cookie传值过程中需要先转成相应的ASCII编码再解析 如下 URLEncoder.encode(& ...

  2. 后台动态生成GridView列和模版

    考虑到很多数据源是不确定的,所以这时无法在前台设置gridview的表头,需要在后台动态指定并绑定数据. 前台代码如下: <%@ Page Title="主页" Langua ...

  3. 【软件推荐】 Moom-窗口布局软件(V.3.2.2)【破解+汉化】

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”     下载地址: 软件:https://yunpan.cn/cuTg8rCMTsLT6 ...

  4. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  5. BZOJ1493 [NOI2007]项链工厂

    未完待续... 终于改对了 热泪盈眶.jpg 错误原因:pushdown的时候没有判断是否有左右儿子,也没当x=0 return,于是出现一些奇怪的错误 #include<bits/stdc++ ...

  6. 配置ssl访问(https)

    转载自http://www.blogjava.net/stevenjohn/archive/2012/09/26/388600.html 简要记录主要步骤备忘 1.进入到jdk下的bin目录 2.输入 ...

  7. CI 资源文件加入模板

    CI  资源文件加入模板: (资源文件:图片,css,js ,业务文件csv,txt.....) 1.引入url辅助函数库   helper 2.使用base_url函数  生成文件物理硬盘地址 3. ...

  8. 合同主体列表添加两条合同主体,返回合并支付页面,支付总弹"请选择合同主体",删除后,竟然还能支付(改合并支付页面的字段状态)

    bug描述: 操作步骤:1.进入"商标续展"产品详情页面,点击立即购买(数量设为2),进入合并订单界面,选择合同主体,点击全部,清空所有合同主体2.新建合同主体保存,设置该合同主体 ...

  9. Cross join in excel --- Copy from Internet

    Set up the Workbook In this example, there are two tables -- Raw Materials and Packaging -- and each ...

  10. ODP方式,大批量数据写入ORACLE数据库

    项目中在同步数据的时候,需要把获得的数据DataTable,写入oracle数据库 因为System.Data.OracleClient写入方式写入大批量数据特别慢,改用Oracle.DataAcce ...