最后更新

二刷

11-Jan-2017

看看给的范围可以重复多少次某个句子。

还是用的第一次的做法,统计2个东西。

1)以某一个单词为一行的第一个单词,那么这一行可以重复多少次整个句子。

2)以某一个单词为一行的第一个单词,那么下一行该由哪个单词开始。

repeatTimes[i] 表示以句子中的第i个单词为开始,句子可以重复多烧瓷。

nextStart[i]表示,下一行的第一个是句子中的哪一个单词。

Time Complexity:

单词个数:N

一开始计算2个array是 O(N * Cols)

后面填充计算总数是O(rows)

所以总共 O(N * cols + rows)

Space:

O(N)

public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols) {
if (sentence.length == 0) return 0;
int length = sentence.length;
int[] repeatTimes = new int[length];
int[] nextStart = new int[length]; for (int i = 0; i < length; i++) {
String tempStr = sentence[i];
int count = 0;
int j = i;
int tempLength = tempStr.length() + 1;
while (tempLength - 1 <= cols) { if (++j == length) {
count ++;
j = 0;
}
tempLength += sentence[j].length() + 1;
}
repeatTimes[i] = count;
nextStart[i] = j;
} int res = 0;
int startWordIndex = 0;
for (int i = 0; i < rows; i++) {
res += repeatTimes[startWordIndex];
startWordIndex = nextStart[startWordIndex];
}
return res;
}
}

一刷

10-Oct-2016

首先想到的是直接做,然后TLE。

public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols)
{
int m = sentence.length;
int res = 0;
int c = 0;
int left = cols;
for(int i = 0; i < rows;)
{
if(sentence[c%m].length() <= left)
{
if(c%m == m-1) res++;
c++;
left = left - sentence[(c-1)%m].length() - 1; }
else
{
i++;
left = cols;
}
} return res; }
}

上面这个在某些TEST CASE的时候发现,其实是有循环出现的,比如

["f","p","a"]

8

7

每3行FPA会出现4次,第四行开始又是重新一边,这样可以直接按3的倍数跳到最后。但是需要注意的是循环的开始不一定就是第一行:

["I", "had", "apple", "pie"]

4

5

循环节是:

apple

pie-I

had--

不是从I开始。

所以要记住开始循环的位置。。

主要说一下DISCUSS里的另一种方法。

整个句子,理论上说每一个单词都有可能作为某一行的开头,我们要计算一下如果以某个单词开头,会是什么结果。结果包括两部分:

1)这一行能塞多少个句子。

2)下一行开头会以什么单词开头。

知道这2个情况之后,以此塞满所有ROWS就行了。

(https://discuss.leetcode.com/topic/62364/java-optimized-solution-17ms)

public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols)
{
int[] times = new int[sentence.length];
int[] nextOne = new int[sentence.length]; for(int i = 0; i < sentence.length; i++)
{
int index = i;
int curLength = 0;
int time = 0;
while(cols >= sentence[index].length() + curLength)
{
curLength += sentence[index].length()+1;
index++;
if(index == sentence.length)
{
index = 0;
time++;
}
} times[i] = time;
nextOne[i] = index;
} int res = 0;
int next = 0;
for(int i = 0; i < rows;i++)
{
res += times[next];
next = nextOne[next];
} return res;
/*
["a","b","e"]
20000
20000
["a", "bcd", "e"]
3
6
["I", "had", "apple", "pie"]
4
5
["a"]
20000
20000
["f","p","a"]
8
7
["try","to","be","better"]
10000
9001
*/ }
}

418. 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. 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 ...

  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 All in One 题目讲解汇总(持续更新中...)

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

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

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

  6. All LeetCode Questions List 题目汇总

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

  7. 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 ...

  8. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  9. 断电不断网——Linux的screen

    title: 断电不断网--Linux的screen author:青南 date: 2015-01-01 20:20:23 categories: [Linux] tags: [linux,scre ...

随机推荐

  1. iOS常见问题(3)

    一.发现不少人在给成员变量初始化的时候,容易进错一个方法去初始化. //注意这个方法只有在内存发生警告的时候才会调用. - (void)didReceiveMemoryWarning { [super ...

  2. GDAL编译(转)

    一.简单的编译 1.使用VisualStudio IDE编译 首先进入GDAL的源代码目录,可以看到有几个sln为后缀的文件名,比如makegdal10.sln,makegdal80.sln,make ...

  3. Careercup - Facebook面试题 - 5110993575215104

    2014-04-30 16:12 题目链接 原题: The beauty of a number X is the number of 1s in the binary representation ...

  4. C#之多态

    多态是面向对象编程中三大机制之一,其原理建立在"从父类继承而来的子类可以转换为其父类"这个规则之上,换句话说,能用父类的地方,就能用该类的子类.当从父类派生了很多子类时,由于每个子 ...

  5. 关于拓扑排序(topologicalsort)

    假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...

  6. Uyuw's Concert POJ2451

    裸半平面交,以前没写过,先写一遍再说 我越来越不注意细节了,最后才发现空间稍微开小了(没有开那个零头,他又要多4条边,就WA了) const maxn=; eps=1e-7; type point=r ...

  7. 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  8. CC150 上面重要的题目总结

    第一章 : 全部重要 (1.6, 1.7 Leetcode上有). 1.5 面A碰到 (string compression) 1.7面Z碰到 (set 0) 1.8面Bigfish碰到 (strin ...

  9. SDUT1574组合数的计算(组合数)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1574 这个题,比较奇怪,是用递推去做的,我试了 ...

  10. POJ2485Highways

    http://poj.org/problem?id=2485 题意 : 这道题和1258很像,但是这道题求的是最小生成树中最大的那条边,所以在函数里处理一下就行了. 思路 : 赤裸裸的最小生成树啊,但 ...