Advertisement
jgsp2

Untitled

Oct 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     unordered_map<char,int> f;
  4.     vector<pair<int,char>> need;
  5.    
  6.     bool check(int wind, string s){
  7.         unordered_map<char,int> d;
  8.         queue<char> q;
  9.         for(char c : s){
  10.             if(q.size()==wind){
  11.                 d[q.front()]--;
  12.                 q.pop();
  13.             }
  14.             d[c]++;
  15.             q.push(c);
  16.            
  17.             //checar aqui
  18.             int n = s.size();
  19.             bool chec = true;
  20.             for(auto p : need){
  21.                 chec &= d[p.second] >= p.first;
  22.             }
  23.             if(chec) return true;
  24.            
  25.         }
  26.        
  27.         return false;
  28.     }
  29.    
  30.     int balancedString(string &s) {
  31.         f['Q']=f['W']=f['E']=f['R'];
  32.        
  33.         for(char c : s)
  34.             f[c]++;
  35.        
  36.  
  37.         if(f['Q']==f['W'] and f['Q']==f['W'] and f['Q']==f['R']) return 0;
  38.        
  39.         int n = s.size();
  40.         for(auto p : f)
  41.             need.push_back({p.second-n/4,p.first});
  42.        
  43.         int l=0,r=s.size();
  44.         for(auto p : need) if(p.first>0)l+=p.first;
  45.  
  46.         while(l<r){
  47.             int mid = (l+r)/2;
  48.             if(check(mid, s)) r=mid;
  49.             else l = mid+1;
  50.         }
  51.         return l;
  52.     }
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement