RainX_69

FIND MAXIMUM CONTINOUS SEGEMENT AFTER ATMOST K REMOVALS (IMPORTANT)

Jan 19th, 2023 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | Source Code | 0 0
  1. HackerLand Sports Club wants to send a team for a relay race. There are n racers in the group indexed from O to n= 1. The
  2. i racer has a speed of speed[i] units.
  3.  
  4. The coach decided to send some contiguous subsegments of racers for the race, such that each racer has the same speed in the group to ensure smooth baton transfer. To achieve the goal, the coach decided to remove some racers from the group such that the number of racers with the same speed In some contiguous segment is maximum:
  5.  
  6. Given the array, racers; and an integer k, find the maximum possible number of racers In some contiguous segment of racers
  7. with the same speed after at most k racers are removed.
  8.  
  9. Example
  10. Suppose n= 6, k= 2, and speed=[1, 4, 4, 2, 2.4].
  11.  
  12. It is optimal to remove the two racers with speed 2 to get the racers [1, 4, 4, 4]. Each racer with speed 4 can now be sent as they
  13. are In 9 contiguous segment. A maximum of 3 racers can be sent for the relay race.
  14.  
  15. Function Description
  16.  
  17. Complete the function getMaxRacers in the editor below.
  18.  
  19. getMaxRacers has the following parameter(s):
  20. int speed[i]: the speeds of the racers
  21. int k: the maximum number of racers that can be removed
  22.  
  23. Returns:
  24. int the maximum number of racers that can be sent after removing at most k racers
  25.  
  26. Constraints
  27. 1<=n<=3*10^5
  28. 1<=k<=n
  29.  
  30. Example
  31.  
  32. speed = [1, 3, 2, 2, 1, 1, 2]
  33. k=3
  34.  
  35. Sample Output
  36. 3
  37.  
  38. Explanation
  39. It is optimal to remove the last second and third racers to get [1, 3, 2, 2, 2]. Note that even removing 2 racers reaches the
  40. optimal possible number of racers that can be sent. In this case, the last three racers can be sent now as they have the same
  41.  
  42. ---------------------------------------------------------------------------------------------------------------------------------------
  43.  
  44. HERE IS A SLIDING WINDOW APPROACH
  45.  
  46. You need to collect all indices for the same element together and then standard sliding window on them. This is because you cannot apply direct sliding window without seperating indices since removing atmost k elements means, you can remove any k elements in a window. The elements under removal consideration might be all distinct so it is hard to trace in a normal sliding window. So seperate indices and apply SW on them, since then you can consider the elements between two indices under removal consideration
  47.  
  48. NICE PROBLEM INDEED :D
  49.  
  50. ---------------------------------------------------------------------------------------------------------------------------------------
  51.  
  52. #include<bits/stdc++.h>
  53. using namespace std;
  54.  
  55. int slidingWindow(vector<int> &indices, int k){
  56.     int wE=0;
  57.     int wS=0;
  58.     int res=0;
  59.     while(wE<indices.size()){
  60.         int totalWinLen=indices[wE]-indices[wS]+1; // the total length of the window in main array
  61.         int continousLen=wE-wS+1;   // this is your continuous window after removing elements that occur in between
  62.         int delReq=totalWinLen-continousLen;      // this is the removals required to achieve continuous Len
  63.         if(delReq>k){
  64.             wS++;
  65.         }
  66.         else{
  67.             res=max(res,wE-wS+1);
  68.             wE++;        
  69.         }
  70.     }
  71.     return res;
  72. }
  73.  
  74. int getMaxRacers(vector<int> &speed, int k) {
  75.     int n=speed.size();
  76.     int res=0;
  77.     unordered_map<int,vector<int>> mpp;
  78.     for(int i=0;i<n;i++){
  79.         mpp[speed[i]].push_back(i);
  80.     }
  81.     for(auto m: mpp){
  82.         res=max(res,slidingWindow(m.second,k));
  83.     }
  84.     return res;
  85. }
  86.  
  87. int main(){
  88.     vector<int> vec;
  89.     int k;
  90.    
  91.     // vec={1,2,1,2,2,1,2,2};
  92.     // k=1;
  93.    
  94.    
  95.     // vec={1,3,2,2,1,1,2};
  96.     // k=3;
  97.    
  98.    
  99.     // vec={1,2,1,2,2,1,2,2};
  100.     // k=2;
  101.    
  102.    
  103.     // vec={1,2,3,1,2,4,1,4,3,4,5,6,4,5,6};
  104.     // k=5;
  105.  
  106.    
  107.     cout<<getMaxRacers(vec,k);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment