Maximum Font to Fit a Sentence in a Screen - Problem
Maximum Font to Fit a Sentence in a Screen
Imagine you're designing a smart display system that automatically adjusts text size to fit perfectly on any screen! ๐ฑ๐ป
You have a
You're given a
โข
โข
Key constraints:
โข Text is displayed on a single line
โข Font dimensions are monotonic (larger font = larger dimensions)
โข Total text width = sum of all character widths
โข Text height = font height
Return the maximum font size that fits, or
Imagine you're designing a smart display system that automatically adjusts text size to fit perfectly on any screen! ๐ฑ๐ป
You have a
text string that needs to be displayed on a screen with dimensions width w and height h. You can choose from an array of available fonts (sorted in ascending order). The challenge is to find the largest possible font size that allows the entire text to fit on the screen.You're given a
FontInfo interface with two methods:โข
getWidth(fontSize, char) - Returns the width of a specific character at given font sizeโข
getHeight(fontSize) - Returns the height of any character at given font sizeKey constraints:
โข Text is displayed on a single line
โข Font dimensions are monotonic (larger font = larger dimensions)
โข Total text width = sum of all character widths
โข Text height = font height
Return the maximum font size that fits, or
-1 if impossible with any available font. Input & Output
example_1.py โ Basic fitting case
$
Input:
text = "helloworld", w = 80, h = 20, fonts = [6,8,10,12,14,16,18,24,36]
โบ
Output:
16
๐ก Note:
Font size 16 fits within the 80ร20 screen (assuming typical character widths), but size 18 would exceed either width or height constraints.
example_2.py โ Edge case - single character
$
Input:
text = "A", w = 20, h = 20, fonts = [10,15,20,25]
โบ
Output:
20
๐ก Note:
Even with a single character, we need to find the largest font that fits both width and height constraints.
example_3.py โ No font fits
$
Input:
text = "verylongtext", w = 10, h = 5, fonts = [12,14,16,18]
โบ
Output:
-1
๐ก Note:
The screen is too small to fit the text with any available font size, so we return -1.
Visualization
Tap to expand
Understanding the Visualization
1
Identify the search space
We have a sorted array of available font sizes
2
Apply binary search
Test middle font size - if it fits, try larger; if not, try smaller
3
Calculate text dimensions
For each test, sum character widths and get font height
4
Check constraints
Verify both width โค screen width and height โค screen height
5
Converge to answer
Continue binary search until finding the maximum fitting size
Key Takeaway
๐ฏ Key Insight: The monotonic property of font dimensions makes binary search perfect for this problem - if a larger font doesn't fit, no even larger font will fit either, allowing us to efficiently eliminate half the search space in each iteration!
Time & Space Complexity
Time Complexity
O(log n * m)
log n binary search iterations ร m characters for width calculation
โก Linearithmic
Space Complexity
O(1)
Only using constant extra space for binary search variables
โ Linear Space
Constraints
- 1 โค text.length โค 5 ร 104
- text only contains lowercase English letters
- 1 โค w, h โค 107
- 1 โค fonts.length โค 105
- 1 โค fonts[i] โค 105
- fonts is sorted in ascending order and all elements are unique
- 1 โค FontInfo.getWidth(fontSize, ch) โค fontSize
- 1 โค FontInfo.getHeight(fontSize) โค fontSize
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code