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 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 size

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 -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
๐Ÿ“ฑ Smart Font Sizing Algorithm๐Ÿ“บ Screen (w=350, h=200)Hello WorldSize 32: Too wide!Hello Worldโœ“ Size 20: Perfect fit!Hello WorldSize 16: Also fits๐Ÿ” Binary Search ProcessStep 1: Initial RangeFonts: [8, 12, 16, 20, 24, 28, 32]Range: Left=0, Right=6, Mid=3 โ†’ Test size 20Step 2: Size 20 Fits!โœ“ Width: 180 โ‰ค 350, Height: 20 โ‰ค 200Update result=20, search larger: Left=4Step 3: Size 28 Too Bigโœ— Width: 392 > 350 (exceeds screen width)Search smaller: Right=4Step 4: Final CheckTest size 24: Width=336 โ‰ค 350 โœ“Update result=24, Left > Right โ†’ Done!๐ŸŽฏ Answer: Font Size 24Maximum size that fits within constraints๐Ÿ’ก Key Insight: Binary search works because font dimensions are monotonic - if size X doesn't fit, no larger size will fit!
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

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space for binary search variables

n
2n
โœ“ 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
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 15
32.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen