-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path961.number_of_lines.py
46 lines (41 loc) · 1.25 KB
/
961.number_of_lines.py
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
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
# @Time : 2019/2/25 11:46
# @Author : xulzee
# @Email : [email protected]
# @File : 961.number_of_lines.py
# @Software: PyCharm
import math
class Solution:
def numberOfLines1(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
count = 0
for i in S:
if count % 100 + widths[ord(i) - 97] > 100:
count = math.ceil(count / 100) * 100 + widths[ord(i) - 97]
else:
count += widths[ord(i) - 97]
return [math.ceil(count / 100), count % 100]
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
lines = 1
linewidth = 0
ord_a = ord('a')
for ch in S:
width = widths[ord(ch) - ord_a]
linewidth += width
if linewidth > 100:
lines += 1
linewidth = width
return [lines, linewidth]
if __name__ == '__main__':
widths = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
S = "abcdefghijklmnopqrstuvwxyz"
print(Solution().numberOfLines(widths, S))