Skip to content

1618. Maximum Font to Fit a Sentence in a Screen 👍

  • Time: $O(|\texttt{text}| + 26\log |\texttt{fonts}|)$
  • Space: $O(26) = O(1)$
 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
/**
 * // This is the FontInfo's API interface.
 * // You should not implement it, or speculate about its implementation
 * class FontInfo {
 *  public:
 *   // Return the width of char ch when fontSize is used.
 *   int getWidth(int fontSize, char ch);
 *
 *   // Return Height of any char when fontSize is used.
 *   int getHeight(int fontSize)
 * };
 */
class Solution {
 public:
  int maxFont(string text, int w, int h, vector<int>& fonts,
              FontInfo fontInfo) {
    vector<int> count(26);

    for (const char c : text)
      ++count[c - 'a'];

    int l = 0;
    int r = fonts.size() - 1;

    while (l < r) {
      const int m = (l + r + 1) / 2;
      if (fontInfo.getHeight(fonts[m]) <= h &&
          getWidthSum(count, fonts[m], fontInfo) <= w)
        l = m;
      else
        r = m - 1;
    }

    return getWidthSum(count, fonts[l], fontInfo) <= w ? fonts[l] : -1;
  }

 private:
  int getWidthSum(const vector<int>& count, int font, FontInfo& fontInfo) {
    int width = 0;
    for (int i = 0; i < 26; ++i)
      width += count[i] * fontInfo.getWidth(font, (char)('a' + i));
    return width;
  }
};
 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
/**
 * // This is the FontInfo's API interface.
 * // You should not implement it, or speculate about its implementation
 * interface FontInfo {
 *   // Return the width of char ch when fontSize is used.
 *   public int getWidth(int fontSize, char ch) {}
 *   // Return Height of any char when fontSize is used.
 *   public int getHeight(int fontSize)
 * }
 */
class Solution {
  public int maxFont(String text, int w, int h, int[] fonts, FontInfo fontInfo) {
    int[] count = new int[26];

    for (final char c : text.toCharArray())
      ++count[c - 'a'];

    int l = 0;
    int r = fonts.length - 1;

    while (l < r) {
      final int m = (l + r + 1) / 2;
      if (fontInfo.getHeight(fonts[m]) <= h && getWidthSum(count, fonts[m], fontInfo) <= w)
        l = m;
      else
        r = m - 1;
    }

    return getWidthSum(count, fonts[l], fontInfo) <= w ? fonts[l] : -1;
  }

  private int getWidthSum(int[] count, int font, FontInfo fontInfo) {
    int width = 0;
    for (int i = 0; i < 26; ++i)
      width += count[i] * fontInfo.getWidth(font, (char) ('a' + i));
    return width;
  }
}
 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
# """
# This is FontInfo's API interface.
# You should not implement it, or speculate about its implementation
# """
# class FontInfo(object):
#   Return the width of char ch when fontSize is used.
#   def getWidth(self, fontSize: int, ch: str) -> int:
#     pass
#
#   def getHeight(self, fontSize: int) -> int:
#     pass
class Solution:
  def maxFont(
      self,
      text: str,
      w: int,
      h: int,
      fonts: list[int],
      fontInfo: 'FontInfo',
  ) -> int:
    count = collections.Counter(text)
    l = 0
    r = len(fonts) - 1

    while l < r:
      m = (l + r + 1) // 2
      if fontInfo.getHeight(
              fonts[m]) <= h and self._getWidthSum(
              count, fonts[m],
              fontInfo) <= w:
        l = m
      else:
        r = m - 1

    return fonts[l] if self._getWidthSum(count, fonts[l], fontInfo) <= w else -1

  def _getWidthSum(
      self,
      count: list[int],
      font: int,
      fontInfo: 'FontInfo',
  ) -> int:
    width = 0
    for c in string.ascii_lowercase:
      width += count[c] * fontInfo.getWidth(font, c)
    return width