-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0418-entence-screen-fitting.py
More file actions
34 lines (27 loc) · 1 KB
/
Copy path0418-entence-screen-fitting.py
File metadata and controls
34 lines (27 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# https://leetcode.com/problems/sentence-screen-fitting/submissions/
class Solution:
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
row = 0
words = 0
lenSent = len(sentence)
firstWords = {}
while(row < rows):
firstWord = sentence[words%lenSent]
# If in cache
if firstWord in firstWords:
words += firstWords[firstWord]
row+=1
continue
# Else - calculate
sentCount = 0
col = 0
while (col < cols):
curWordLen = int(bool(col)) * 1 + len(sentence[words%lenSent])
if col + curWordLen <= cols:
col += curWordLen
words += 1
sentCount += 1
else: break
firstWords[firstWord] = sentCount
row+=1
return words//lenSent