Question: DE Shaw, Online Assesment (DESIS | 17th September 2023) | Range Selection | GetMinRemoval
1
Entering edit mode

ADD COMMENTlink 15 months ago Delta 2.9k
1
Entering edit mode

2. Range Selection

bool ok(vector<ll> starts,ll d,ll m){
    ll st=starts[0]+m;
    for(int i=1;i<starts.size();i++){
        st=max(st,starts[i]);
        if(starts[i]+d<st) return false;
        st+=m;
    }
    return true;
}

ll solve(vector<ll> &starts,ll d){
    sort(starts.begin(),starts.end());
    ll l=0,r=1e15;
    while(l<=r){
        ll m=(l+r)/2;
        if(ok(starts,d,m)){
            l=m+1;
        }else r=m-1;
    }
    return r;
}

ADD COMMENTlink 15 months ago Shristi Chandrakar • 40
0
Entering edit mode

1. GetMinRemoval

    string s;

    cin>>s;

    int k;

    cin>>k;

 

    vector<int> freq(26);

    for(int i=0;i<s.size();i++) {

        freq[s[i]-'a']++;

    }

 

    sort(freq.begin(),freq.end());

    int i=0,j=freq.size()-1;

 

    int ans = 0;

    while(i<=j) {

        if(freq[i]==0) i++;

        else if(freq[j]==0) j--;

        else if(abs(freq[i]-freq[j])>k) {

            ans += abs(freq[i]-freq[j])-k;

            j--;

        } else break;

    }

    cout<<ans<<endl;

ADD COMMENTlink 13 months ago Tarun • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts