418. Sentence Screen Fitting

Description

Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.

Note:

  1. A word cannot be split into two lines.
  2. The order of words in the sentence must remain unchanged.
  3. Two consecutive words in a line must be separated by a single space.
  4. Total words in the sentence won't exceed 100.
  5. Length of each word is greater than 0 and won't exceed 10.
  6. 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.

Solution

好迷啊耗溜,不太懂肘交,to be added...

class Solution {
    public int wordsTyping(String[] words, int rows, int cols) {
        String sentence = String.join(" ", words) + " ";    // add a space between two sentences
        int len = sentence.length();
        // think there are a lot of same sentences connected
        int start = 0;  // current position in the sentences group
        
        for (int i = 0; i < rows; ++i) {
            start += cols;  // start now point to the fist char in the new row
            if (sentence.charAt(start % len) == ' ') {  // remove space in the beginning of the row
                ++start;
            } else {    // if the beginning is not a space, it must be a broken word
                // move the broken word from prev line to the new line
                while (start > 0 && sentence.charAt((start - 1) % len) != ' ') {
                    --start;
                }
                // start will get back to 0 if there's a long word won't fit screen
            }
        }
        
        return start / len;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末卒暂,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子泌类,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,865評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件革答,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡曙强,警方通過(guò)查閱死者的電腦和手機(jī)残拐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)旗扑,“玉大人蹦骑,你說(shuō)我怎么就攤上這事⊥畏溃” “怎么了眠菇?”我有些...
    開封第一講書人閱讀 169,631評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)袱衷。 經(jīng)常有香客問我捎废,道長(zhǎng),這世上最難降的妖魔是什么致燥? 我笑而不...
    開封第一講書人閱讀 60,199評(píng)論 1 300
  • 正文 為了忘掉前任登疗,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辐益。我一直安慰自己断傲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,196評(píng)論 6 398
  • 文/花漫 我一把揭開白布智政。 她就那樣靜靜地躺著认罩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪续捂。 梳的紋絲不亂的頭發(fā)上垦垂,一...
    開封第一講書人閱讀 52,793評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音牙瓢,去河邊找鬼劫拗。 笑死,一個(gè)胖子當(dāng)著我的面吹牛矾克,可吹牛的內(nèi)容都是我干的页慷。 我是一名探鬼主播,決...
    沈念sama閱讀 41,221評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼聂渊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼差购!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起汉嗽,我...
    開封第一講書人閱讀 40,174評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤欲逃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后饼暑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稳析,經(jīng)...
    沈念sama閱讀 46,699評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,770評(píng)論 3 343
  • 正文 我和宋清朗相戀三年弓叛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了彰居。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,918評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡撰筷,死狀恐怖陈惰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情毕籽,我是刑警寧澤抬闯,帶...
    沈念sama閱讀 36,573評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站关筒,受9級(jí)特大地震影響溶握,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蒸播,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,255評(píng)論 3 336
  • 文/蒙蒙 一睡榆、第九天 我趴在偏房一處隱蔽的房頂上張望萍肆。 院中可真熱鬧,春花似錦胀屿、人聲如沸塘揣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)勿负。三九已至馏艾,卻和暖如春劳曹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琅摩。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工铁孵, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人房资。 一個(gè)月前我還...
    沈念sama閱讀 49,364評(píng)論 3 379
  • 正文 我出身青樓蜕劝,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親轰异。 傳聞我的和親對(duì)象是個(gè)殘疾皇子岖沛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,926評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,350評(píng)論 0 10
  • Given a rows x cols screen and a sentence represented by ...
    Jeanz閱讀 521評(píng)論 0 0
  • 當(dāng)很多人看到這個(gè)標(biāo)題的時(shí)候,也許會(huì)有一半的人認(rèn)為我寫的是愛情搭独!不婴削,我寫的是友情。 很多人都有閨蜜吧牙肝,那你們覺得自己...
    三歲的晴小晴閱讀 254評(píng)論 0 0
  • 一口氣撐著不睡把經(jīng)濟(jì)生活二分之一的計(jì)算題做了兩遍
    無(wú)言好名字閱讀 119評(píng)論 0 0
  • 從你的全世界路過(guò)唉俗,等不到的人就不要再等了,每個(gè)人都有屬于自己的路配椭,都有選擇自己未來(lái)的權(quán)利虫溜,你無(wú)能為力,只能在心中默...
    每天努力一點(diǎn)YYY閱讀 121評(píng)論 0 1