Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- i racer has a speed of speed[i] units.
- 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:
- Given the array, racers; and an integer k, find the maximum possible number of racers In some contiguous segment of racers
- with the same speed after at most k racers are removed.
- Example
- Suppose n= 6, k= 2, and speed=[1, 4, 4, 2, 2.4].
- 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
- are In 9 contiguous segment. A maximum of 3 racers can be sent for the relay race.
- Function Description
- Complete the function getMaxRacers in the editor below.
- getMaxRacers has the following parameter(s):
- int speed[i]: the speeds of the racers
- int k: the maximum number of racers that can be removed
- Returns:
- int the maximum number of racers that can be sent after removing at most k racers
- Constraints
- 1<=n<=3*10^5
- 1<=k<=n
- Example
- speed = [1, 3, 2, 2, 1, 1, 2]
- k=3
- Sample Output
- 3
- Explanation
- 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
- 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
- ---------------------------------------------------------------------------------------------------------------------------------------
- HERE IS A SLIDING WINDOW APPROACH
- 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
- NICE PROBLEM INDEED :D
- ---------------------------------------------------------------------------------------------------------------------------------------
- #include<bits/stdc++.h>
- using namespace std;
- int slidingWindow(vector<int> &indices, int k){
- int wE=0;
- int wS=0;
- int res=0;
- while(wE<indices.size()){
- int totalWinLen=indices[wE]-indices[wS]+1; // the total length of the window in main array
- int continousLen=wE-wS+1; // this is your continuous window after removing elements that occur in between
- int delReq=totalWinLen-continousLen; // this is the removals required to achieve continuous Len
- if(delReq>k){
- wS++;
- }
- else{
- res=max(res,wE-wS+1);
- wE++;
- }
- }
- return res;
- }
- int getMaxRacers(vector<int> &speed, int k) {
- int n=speed.size();
- int res=0;
- unordered_map<int,vector<int>> mpp;
- for(int i=0;i<n;i++){
- mpp[speed[i]].push_back(i);
- }
- for(auto m: mpp){
- res=max(res,slidingWindow(m.second,k));
- }
- return res;
- }
- int main(){
- vector<int> vec;
- int k;
- // vec={1,2,1,2,2,1,2,2};
- // k=1;
- // vec={1,3,2,2,1,1,2};
- // k=3;
- // vec={1,2,1,2,2,1,2,2};
- // k=2;
- // vec={1,2,3,1,2,4,1,4,3,4,5,6,4,5,6};
- // k=5;
- cout<<getMaxRacers(vec,k);
- }
Advertisement
Add Comment
Please, Sign In to add comment