forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-font-to-fit-a-sentence-in-a-screen.py
46 lines (39 loc) · 1.19 KB
/
maximum-font-to-fit-a-sentence-in-a-screen.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
# Time: O(n + logm), n is the length of text, m is the number of fonts
# Space: O(1)
import collections
class FontInfo(object):
def getWidth(self, fontSize, ch):
"""
:type fontSize: int
:type ch: char
:rtype int
"""
pass
def getHeight(self, fontSize):
"""
:type fontSize: int
:rtype int
"""
pass
class Solution(object):
def maxFont(self, text, w, h, fonts, fontInfo):
"""
:type text: str
:type w: int
:type h: int
:type fonts: List[int]
:type fontInfo: FontInfo
:rtype: int
"""
def check(count, w, h, fonts, fontInfo, x): # Time: O(1)
return (fontInfo.getHeight(fonts[x]) <= h and
sum(cnt * fontInfo.getWidth(fonts[x], c) for c, cnt in count.iteritems()) <= w)
count = collections.Counter(text)
left, right = 0, len(fonts)-1
while left <= right:
mid = left + (right-left)//2
if not check(count, w, h, fonts, fontInfo, mid):
right = mid-1
else:
left = mid+1
return fonts[right] if right >= 0 else -1