Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void optimalDigitsRemoval(const string& s, int K) {
- vector < int > freq(10);
- int N = s.size(), stop = N;
- for(int i = 0; i < N; ++i) {
- int cif = s[i] - '0';
- for(int j = 0; j < cif; ++j)
- if(K >= freq[j]) {
- K -= freq[j];
- freq[j] = 0;
- }
- else {
- freq[j] -= K;
- K = 0;
- }
- if(K == 0) {
- stop = i;
- break;
- }
- ++freq[cif];
- }
- if(K > 0) {
- for(int i = 0; i < 10 && K > 0; ++i)
- if(K >= freq[i]) {
- K -= freq[i];
- freq[i] = 0;
- }
- else {
- freq[i] -= K;
- K = 0;
- }
- }
- for(int i = 9; i >= 0; --i)
- for(int j = 0; j < freq[i]; ++j)
- cout << i;
- for(int i = stop; i < N; ++i)
- cout << s[i];
- }
Advertisement
Add Comment
Please, Sign In to add comment