1618.找出适应屏幕的最大字号

时游小于 1 分钟LeetCode

1618.找出适应屏幕的最大字号

/**
 * // 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}
 */

const check = (text, w, h, size, fontInfo) => {
      let height =  fontInfo.getHeight(size) 
      if(height > h ) return false 
      return [...text].reduce((a,b) => a + fontInfo.getWidth(size,b), 0) <= w
}


var maxFont = function(text, w, h, fonts, fontInfo) {
    let l = 0, r = fonts.length - 1
    while(l <= r){
        let m =  Math.ceil((l + r) / 2);
        if(check(text, w, h, fonts[m], fontInfo)) l = m + 1
        else r = m - 1 
    }
    return r < 0 ? -1 : fonts[r]
};
上次编辑于:
贡献者: 15327360835
Loading...