Advertisement
nikunjsoni

1618

May 21st, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. /**
  2.  * // This is the FontInfo's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class FontInfo {
  5.  *   public:
  6.  *     // Return the width of char ch when fontSize is used.
  7.  *     int getWidth(int fontSize, char ch);
  8.  *    
  9.  *     // Return Height of any char when fontSize is used.
  10.  *     int getHeight(int fontSize)
  11.  * };
  12.  */
  13. class Solution {
  14. public:
  15.     int maxFont(string text, int w, int h, vector<int>& fonts, FontInfo fontInfo) {
  16.         int l=0, r=fonts.size();
  17.        
  18.         while(l<r){
  19.             int mid = (l+r)/2;
  20.             int height = fontInfo.getHeight(fonts[mid]);
  21.             if(height > h || getWidth(fontInfo, fonts[mid], text) > w){
  22.                 r = mid;
  23.             }
  24.             else{
  25.                 l = mid+1;
  26.             }
  27.         }
  28.         if(!r) return -1;
  29.         return fonts[r-1];
  30.     }
  31.    
  32.     int getWidth(FontInfo f, int fontSize, string &txt){
  33.         int width = 0;
  34.         int w[26];
  35.         for(int i=0; i<26; i++)
  36.             w[i] = f.getWidth(fontSize, 'a'+i);
  37.        
  38.         for(int i=0; i<txt.size(); i++)
  39.             width += w[txt[i]-'a'];
  40.        
  41.         return width;
  42.     }
  43.    
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement