Maximum Font to Fit a Sentence in a Screen Problem
Description
LeetCode Problem 1618.
You are given a string text. We want to display text on a screen of width w and height h. You can choose any font size from array fonts, which contains the available font sizes in ascending order.
You can use the FontInfo interface to get the width and height of any character at any available font size.
The FontInfo interface is defined as such:
interface FontInfo {
// Returns the width of character ch on the screen using font size fontSize.
// O(1) per call
public int getWidth(int fontSize, char ch);
// Returns the height of any character on the screen using font size fontSize.
// O(1) per call
public int getHeight(int fontSize);
}
The calculated width of text for some fontSize is the sum of every getWidth(fontSize, text[i]) call for each 0 <= i < text.length (0-indexed). The calculated height of text for some fontSize is getHeight(fontSize). Note that text is displayed on a single line.
It is guaranteed that FontInfo will return the same value if you call getHeight or getWidth with the same parameters.
It is also guaranteed that for any font size fontSize and any character ch:
getHeight(fontSize) <= getHeight(fontSize+1)getWidth(fontSize, ch) <= getWidth(fontSize+1, ch)
Return the maximum font size you can use to display text on the screen. If text cannot fit on the display with any font size, return -1.
Example 1:
Input: text = "helloworld", w = 80, h = 20, fonts = [6,8,10,12,14,16,18,24,36]
Output: 6
Example 2:
Input: text = "leetcode", w = 1000, h = 50, fonts = [1,2,4]
Output: 4
Example 3:
Input: text = "easyquestion", w = 100, h = 100, fonts = [10,15,20,25]
Output: -1
Constraints:
1 <= text.length <= 50000textcontains only lowercase English letters.1 <= w <= 10^71 <= h <= 10^41 <= fonts.length <= 10^51 <= fonts[i] <= 10^5fontsis sorted in ascending order and does not contain duplicates.
Solution
Approach: Binary Search on the Font Index
A font size fits when the text's height is at most h and the sum of its character widths is at most w. Both height and width never decrease as the font grows, so if some size fits, every smaller size fits too. The sizes in fonts are sorted, so the fitting ones form a prefix of the array, and we want the last element of that prefix.
That is a binary search. Keep a range [lo, hi] of indices and look at mid = (lo + hi + 1) / 2:
- If
fonts[mid]fits, the answer is atmidor later:lo = mid. - Otherwise it is before
mid:hi = mid - 1.
Rounding mid up matters: with lo = mid and a rounded-down mid, a range of two elements would never shrink.
When the loop ends, lo is the only candidate, but it may still not fit (when even the smallest font is too large), so check it once more and return -1 if it fails.
Each check asks FontInfo for the width of every character, which is O(text length). Checking the height first skips that work for fonts that are too tall.
Complexity: O(m log f) time, where m is the length of text and f the number of fonts, and O(1) extra space.
class Solution:
def maxFont(self, text: str, w: int, h: int, fonts: List[int], fontInfo: 'FontInfo') -> int:
def fits(size: int) -> bool:
if fontInfo.getHeight(size) > h:
return False
return sum(fontInfo.getWidth(size, c) for c in text) <= w
# Larger fonts never fit better, so binary search the last font that fits.
lo, hi = 0, len(fonts) - 1
while lo < hi:
mid = (lo + hi + 1) // 2
if fits(fonts[mid]):
lo = mid
else:
hi = mid - 1
return fonts[lo] if fits(fonts[lo]) else -1