classSolution{public:intmaximumRobots(vector<int>&chargeTimes,vector<int>&runningCosts,longlongbudget){longcost=0;deque<int>maxQ;// Stores `chargeTimes[i]`.intj=0;// window's range := [i..j], so k = i - j + 1for(inti=0;i<chargeTimes.size();++i){cost+=runningCosts[i];while(!maxQ.empty()&&maxQ.back()<chargeTimes[i])maxQ.pop_back();maxQ.push_back(chargeTimes[i]);if(maxQ.front()+(i-j+1)*cost>budget){if(maxQ.front()==chargeTimes[j])maxQ.pop_front();cost-=runningCosts[j++];}}returnchargeTimes.size()-j;}};
1 2 3 4 5 6 7 8 910111213141516171819202122
classSolution{publicintmaximumRobots(int[]chargeTimes,int[]runningCosts,longbudget){longcost=0;// Stores `chargeTimes[i]`.Deque<Integer>maxQ=newArrayDeque<>();intj=0;// window's range := [i..j], so k = i - j + 1for(inti=0;i<chargeTimes.length;++i){cost+=runningCosts[i];while(!maxQ.isEmpty()&&maxQ.peekLast()<chargeTimes[i])maxQ.pollLast();maxQ.offerLast(chargeTimes[i]);if(maxQ.peekFirst()+(i-j+1)*cost>budget){if(maxQ.peekFirst()==chargeTimes[j])maxQ.pollFirst();cost-=runningCosts[j++];}}returnchargeTimes.length-j;}}
1 2 3 4 5 6 7 8 9101112131415161718192021222324
classSolution:defmaximumRobots(self,chargeTimes:list[int],runningCosts:list[int],budget:int,)->int:cost=0maxQ=collections.deque()# Stores `chargeTimes[i]`.j=0# window's range := [i..j], so k = i - j + 1fori,(chargeTime,runningCost)inenumerate(zip(chargeTimes,runningCosts)):cost+=runningCostwhilemaxQandmaxQ[-1]<chargeTime:maxQ.pop()maxQ.append(chargeTime)ifmaxQ[0]+(i-j+1)*cost>budget:ifmaxQ[0]==chargeTimes[j]:maxQ.popleft()cost-=runningCosts[j]j+=1returnlen(chargeTimes)-j
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π