返回介绍

solution / 1600-1699 / 1618.Maximum Font to Fit a Sentence in a Screen / README_EN

发布于 2024-06-17 01:03:16 字数 7845 浏览 0 评论 0 收藏 0

1618. Maximum Font to Fit a Sentence in a Screen

中文文档

Description

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 <= 50000
  • text contains only lowercase English letters.
  • 1 <= w <= 107
  • 1 <= h <= 104
  • 1 <= fonts.length <= 105
  • 1 <= fonts[i] <= 105
  • fonts is sorted in ascending order and does not contain duplicates.

Solutions

Solution 1

# """
# 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, ch):
#    """
#    :type fontSize: int
#    :type ch: char
#    :rtype int
#    """
#
#  def getHeight(self, fontSize):
#    """
#    :type fontSize: int
#    :rtype int
#    """
class Solution:
  def maxFont(
    self, text: str, w: int, h: int, fonts: List[int], fontInfo: 'FontInfo'
  ) -> int:
    def check(size):
      if fontInfo.getHeight(size) > h:
        return False
      return sum(fontInfo.getWidth(size, c) for c in text) <= w

    left, right = 0, len(fonts) - 1
    ans = -1
    while left < right:
      mid = (left + right + 1) >> 1
      if check(fonts[mid]):
        left = mid
      else:
        right = mid - 1
    return fonts[left] if check(fonts[left]) else -1
/**
 * // 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 left = 0, right = fonts.length - 1;
    while (left < right) {
      int mid = (left + right + 1) >> 1;
      if (check(text, fonts[mid], w, h, fontInfo)) {
        left = mid;
      } else {
        right = mid - 1;
      }
    }
    return check(text, fonts[left], w, h, fontInfo) ? fonts[left] : -1;
  }

  private boolean check(String text, int size, int w, int h, FontInfo fontInfo) {
    if (fontInfo.getHeight(size) > h) {
      return false;
    }
    int width = 0;
    for (char c : text.toCharArray()) {
      width += fontInfo.getWidth(size, c);
    }
    return width <= w;
  }
}
/**
 * // 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) {
    auto check = [&](int size) {
      if (fontInfo.getHeight(size) > h) return false;
      int width = 0;
      for (char& c : text) {
        width += fontInfo.getWidth(size, c);
      }
      return width <= w;
    };
    int left = 0, right = fonts.size() - 1;
    while (left < right) {
      int mid = (left + right + 1) >> 1;
      if (check(fonts[mid])) {
        left = mid;
      } else {
        right = mid - 1;
      }
    }
    return check(fonts[left]) ? fonts[left] : -1;
  }
};
/**
 * // This is the FontInfo's API interface.
 * // You should not implement it, or speculate about its implementation
 * function FontInfo() {
 *
 *    @param {number} fontSize
 *    @param {char} ch
 *     @return {number}
 *     this.getWidth = function(fontSize, ch) {
 *      ...
 *     };
 *
 *    @param {number} fontSize
 *     @return {number}
 *     this.getHeight = function(fontSize) {
 *      ...
 *     };
 * };
 */
/**
 * @param {string} text
 * @param {number} w
 * @param {number} h
 * @param {number[]} fonts
 * @param {FontInfo} fontInfo
 * @return {number}
 */
var maxFont = function (text, w, h, fonts, fontInfo) {
  const check = function (size) {
    if (fontInfo.getHeight(size) > h) {
      return false;
    }
    let width = 0;
    for (const c of text) {
      width += fontInfo.getWidth(size, c);
    }
    return width <= w;
  };
  let left = 0;
  let right = fonts.length - 1;
  while (left < right) {
    const mid = (left + right + 1) >> 1;
    if (check(fonts[mid])) {
      left = mid;
    } else {
      right = mid - 1;
    }
  }
  return check(fonts[left]) ? fonts[left] : -1;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文