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 ...
随机推荐
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Linux文件系统扩容步骤
1 扩容前检查 cat /etc/fstab df -h 在扩容之前请确认VG的Free大小,以及文件和文件系统是否达到系统限制 2 系统识别硬盘 #echo "- - -" &g ...
- dev checkbox多选
GridControl如果要支持多选,设置Options->OptionSeletion->MultiSelet为true就ok.
- sql server 权限体系
--给sql server添加一个新用户[账号,密码,数据库名] execute sp_addlogin 'baishi', '123','db'; execute sp_addlogin 'wx ...
- 后台发送POST,DELETE,GET,PUT请求
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, int> para ...
- 事后分析报告(Postmortem Report)
小组讨论照片 设想和目标 1.我们的团队项目为英语单词学习助手,名为“我爱记单词”.主要提供服务包括:单词查询,单词测试,单词记忆和中英互译.目前开发的是单机版本,用户可以根据自己的需求灵活的使用相应 ...
- JAVA实现复制文件夹
package com.filetest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- 【iCore3应用开发平台】发布 iCore3 应用开发平台寄存器说明
PDF下载地址:http://pan.baidu.com/s/1qYeZROC
- OLAP在大数据时代的挑战
转行做数据相关的工作有近两年时间,除了具体技术,还有许多其它思考. 数据的价值 在涉及具体的技术前,先想一想为什么需要OLAP这样的系统,它有什么价值或者说在公司或部门这是不可取代的么? 可以带来哪些 ...
- [UVa 1619]Feel Good
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; struct node { int num; ...