Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sentence-screen-fitting.py 941 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-12 01:33 +08:00 . remove sensitive question description
# Time: O(r + n * c)
# Space: O(n)
class Solution(object):
def wordsTyping(self, sentence, rows, cols):
"""
:type sentence: List[str]
:type rows: int
:type cols: int
:rtype: int
"""
def words_fit(sentence, start, cols):
if len(sentence[start]) > cols:
return 0
s, count = len(sentence[start]), 1
i = (start + 1) % len(sentence)
while s + 1 + len(sentence[i]) <= cols:
s += 1 + len(sentence[i])
count += 1
i = (i + 1) % len(sentence)
return count
wc = [0] * len(sentence)
for i in xrange(len(sentence)):
wc[i] = words_fit(sentence, i, cols)
words, start = 0, 0
for i in xrange(rows):
words += wc[start]
start = (start + wc[start]) % len(sentence)
return words / len(sentence)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助