​LeetCode刷题实战418:屏幕可显示句子的数量

程序IT圈

共 1715字,需浏览 4分钟

 ·

2021-10-25 18:34

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 屏幕可显示句子的数量,我们先来看题面:
https://leetcode-cn.com/problems/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.



给你一个 rows x cols 的屏幕和一个用 非空 的单词列表组成的句子,请你计算出给定句子可以在屏幕上完整显示的次数。
注意:
一个单词不能拆分成两行。
单词在句子中的顺序必须保持不变。
在一行中 的两个连续单词必须用一个空格符分隔。
句子中的单词总量不会超过 100。
每个单词的长度大于 0 且不会超过 10。
1 ≤ rows, cols ≤ 20,000.

示例


示例 1:
输入:
rows = 2, cols = 8,
句子 sentence = ["hello", "world"]
输出:
1
解释:
hello---
world---
字符 '-' 表示屏幕上的一个空白位置。
 
示例 2:
输入:
rows = 3, cols = 6,
句子 sentence = ["a", "bcd", "e"]
输出:
2
解释:
a-bcd-
e-a---
bcd-e-
字符 '-' 表示屏幕上的一个空白位置。
 
示例 3:
输入:
rows = 4, cols = 5,
句子 sentence = ["I", "had", "apple", "pie"]
输出:
1
解释:
I-had
apple
pie-I
had--
字符 '-' 表示屏幕上的一个空白位置。


解题

https://www.cnblogs.com/FdWzy/p/12406168.html

贪心法:先顺序放入句子。记录每次放完一轮句子,最后一个单词的末尾列号m,假设此时是k1行。如果
之后的遍历中,也是当句子遍历完一轮的时候,最后一个单词的末尾序号也是m,假设此时是k2行(当然k2>k1)。
那么说明k1行m列开始到k2行m列结束,一共有整数个完整的句子。那么往下直接跳过(k2-k1)行到达第k2+(k2-k1)
行的第m列,正好还是另一个句子的结尾。就这样一直往后跳,直到跳到不能再跳(剩余行数不够跳了),再
正常顺序放入句子直到不能再放为止。

class Solution {
public:
    int wordsTyping(vector<string>& sentence, int rows, int cols) {
        unordered_map<int,pair<int,int>> mp;//mp[j]=pair:放入pair[0]个句子、pair[1]行的最后元素的列数为j
        int cur_row=0,cur_col=0,words=sentence.size(),res=0;
        while(cur_row            // cout<
            int i=0;
            while(cur_rowand
 i                if(cur_coland cols-cur_col>=sentence[i].size()){
                    cur_col+=sentence[i++].size()+1;
                }
                else{//换行
                    ++cur_row;
                    cur_col=0;
                }
            }
            if(i                return res;
            }
            ++res;
            if(mp.count(cur_col-1)==0){
                mp[cur_col-1]={res,cur_row};
            }
            else{
                int row_dif=cur_row-mp[cur_col-1].second;
                int sen_dif=res-mp[cur_col-1].first;
                int p=(rows-1-cur_row)/row_dif;
                cur_row+=p*row_dif;
                res+=p*sen_dif;
                mp[cur_col-1]={res,cur_row};
            }
        }
        return res;
    }
};


好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-400题汇总,希望对你有点帮助!

LeetCode刷题实战401:二进制手表

LeetCode刷题实战402:移掉 K 位数字

LeetCode刷题实战403:青蛙过河

LeetCode刷题实战404:左叶子之和

LeetCode刷题实战405:数字转换为十六进制数

LeetCode刷题实战406:根据身高重建队列

LeetCode刷题实战407:接雨水 II

LeetCode刷题实战408:有效单词缩写

LeetCode刷题实战409:最长回文串

LeetCode刷题实战410:分割数组的最大值

LeetCode刷题实战411:最短独占单词缩写

LeetCode刷题实战412:Fizz Buzz

LeetCode刷题实战413:等差数列划分

LeetCode刷题实战414:第三大的数

LeetCode刷题实战415:字符串相加

LeetCode刷题实战416:分割等和子集

LeetCode刷题实战417:太平洋大西洋水流问题


浏览 24
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报