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:
# """# 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# """classSolution:defmaxFont(self,text:str,w:int,h:int,fonts:List[int],fontInfo:'FontInfo')->int:defcheck(size):iffontInfo.getHeight(size)>h:returnFalsereturnsum(fontInfo.getWidth(size,c)forcintext)<=wleft,right=0,len(fonts)-1ans=-1whileleft<right:mid=(left+right+1)>>1ifcheck(fonts[mid]):left=midelse:right=mid-1returnfonts[left]ifcheck(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) * } */classSolution{publicintmaxFont(Stringtext,intw,inth,int[]fonts,FontInfofontInfo){intleft=0,right=fonts.length-1;while(left<right){intmid=(left+right+1)>>1;if(check(text,fonts[mid],w,h,fontInfo)){left=mid;}else{right=mid-1;}}returncheck(text,fonts[left],w,h,fontInfo)?fonts[left]:-1;}privatebooleancheck(Stringtext,intsize,intw,inth,FontInfofontInfo){if(fontInfo.getHeight(size)>h){returnfalse;}intwidth=0;for(charc:text.toCharArray()){width+=fontInfo.getWidth(size,c);}returnwidth<=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) * }; */classSolution{public:intmaxFont(stringtext,intw,inth,vector<int>&fonts,FontInfofontInfo){autocheck=[&](intsize){if(fontInfo.getHeight(size)>h)returnfalse;intwidth=0;for(char&c:text){width+=fontInfo.getWidth(size,c);}returnwidth<=w;};intleft=0,right=fonts.size()-1;while(left<right){intmid=(left+right+1)>>1;if(check(fonts[mid])){left=mid;}else{right=mid-1;}}returncheck(fonts[left])?fonts[left]:-1;}};