LeetCode 418. Sentence Screen Fitting TextView自適應

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.

----------------------------------------------------------------------------

這題目剛開始用循環節的思路搞了,非常容易寫bug,代碼如下:

class Solution:
    def wordsTyping(self, sentence, rows: int, cols: int) -> int:
        dic = {0: (0, 0)}  # off:(linenumber,res)
        x, y, res, idx, wl, flg = 0, 0, 0, 0, len(sentence), False

        while (True):
            l = len(sentence[idx])
            if (l > cols):
                return 0
            newline = 0 if x + l <= cols else 1  # write cur word in new line
            used = x + l + 1 if newline == 0 else l + 1
            nx = used if used < cols else 0
            ny = y + newline if used < cols else y + 1 + newline

            if (ny > rows or (ny == rows and nx > 0)):
                return res
            idx, x, y = idx + 1, nx, ny
            if (idx == wl):
                res += 1
                idx = 0
                if (flg == False):
                    if (x in dic):
                        pline, pres = dic[x]
                        cycle = (rows - y -1) // (y - pline) #bug1: cycle = (rows - pline) // (y - pline)
                        #bug3: cycle = (rows - y) // (y - pline)
                        print("pline={0} pres={1} y={2} res={3} cycle={4}".format(pline,pres, y, res, cycle))
                        res += cycle * (res - pres)
                        y += cycle * (y - pline) #bug2: y += cycle * (rows - pline)
                        flg = True
                    dic[x] = (y, res)

後來在Discussion裏發現了一種更好的思路:

把所有的詞以空格分隔,假設可以拼成無限長度,那麼問題就變成了每次給一行屏幕的寬度cols,展示一定數量的單詞,這個off會到什麼位置,最後off // len(拼起來的sentence)就是需要的結果

class Solution(object):
    def wordsTyping(self, sentence, rows, cols):
        s = ' '.join(sentence) + ' '
        l = len(s)
        off = 0
        for i in range(rows):
            off += cols
            if (s[off % l] == ' '):
                off += 1
            else:
                while (off > 0 and s[(off-1)%l] != ' '):
                    off -= 1
        return off // l

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章