Skip to content

Commit 247cfcc

Browse files
committed
Add 1618
1 parent 1c83c79 commit 247cfcc

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
| 1609 | Even Odd Tree | Easy | O(N) | O(N) | Tree | | |
292292
| 1614 | Maximum Nesting Depth of the Parentheses | Easy | O(N) | O(1) | String | | |
293293
| 1615 | Maximal Network Rank | Medium | O(N) | O(1) | Graph | | |
294+
| 1618 | Maximum Font to Fit a Sentence in a Screen | Medium | O(logN) | O(1) | Premium, String, Binary Search | | 🔒 |
294295
| 1624 | Largest Substring Between Two Equal Characters | Easy | O(N) | O(1) | String | | |
295296
| 1629 | Slowest Key | Easy | O(N) | O(1) | String | | |
296297
| 1634 | Add Two Polynomials Represented as Linked Lists | Medium | O(N) | O(1) | Premium, Linked List | | 🔒 |

src/1618.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# TAGS: Premium, String, Binary Search
2+
# """
3+
# This is FontInfo's API interface.
4+
# You should not implement it, or speculate about its implementation
5+
# """
6+
#class FontInfo(object):
7+
# Return the width of char ch when fontSize is used.
8+
# def getWidth(self, fontSize, ch):
9+
# """
10+
# :type fontSize: int
11+
# :type ch: char
12+
# :rtype int
13+
# """
14+
#
15+
# def getHeight(self, fontSize):
16+
# """
17+
# :type fontSize: int
18+
# :rtype int
19+
# """
20+
class Solution:
21+
# Without counter. 692 ms, 8.6% Time: O(NlogN). Space: O(1)
22+
# With counter. 480 ms, 74.19% Time: O(logN). Space: O(1)
23+
def maxFont(self, text: str, w: int, h: int, fonts: List[int], fontInfo : 'FontInfo') -> int:
24+
counter = collections.Counter(text)
25+
def fit_on_screen(font_size, text):
26+
width = sum(fontInfo.getWidth(font_size, c) * f for c, f in counter.items())
27+
height = fontInfo.getHeight(font_size)
28+
return width <= w and height <= h
29+
30+
lo, hi = 0, len(fonts) - 1
31+
while lo < hi:
32+
mid = (lo + hi + 1) // 2
33+
if fit_on_screen(fonts[mid], text):
34+
lo = mid
35+
else:
36+
hi = mid - 1
37+
38+
return fonts[lo] if fit_on_screen(fonts[lo], text) else -1

testcases/1618

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"helloworld"
2+
80
3+
20
4+
[6,8,10,12,14,16,18,24,36]
5+
"leetcode"
6+
1000
7+
50
8+
[1,2,4]
9+
"easyquestion"
10+
100
11+
100
12+
[10,15,20,25]
13+
"abc"
14+
10
15+
10
16+
[1]
17+
"abc"
18+
10
19+
10
20+
[100]

0 commit comments

Comments
 (0)