forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2398.Maximum-Number-of-Robots-Within-Budget_v3.cpp
- Loading branch information
1 parent
b67fc2c
commit c8fc026
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...Maximum-Number-of-Robots-Within-Budget/2398.Maximum-Number-of-Robots-Within-Budget_v3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using LL = long long; | ||
class Solution { | ||
public: | ||
int maximumRobots(vector<int>& chargeTimes, vector<int>& runningCosts, long long budget) | ||
{ | ||
vector<pair<LL,LL>>robots; | ||
int n = chargeTimes.size(); | ||
|
||
LL left = 0, right = n; | ||
while (left < right) | ||
{ | ||
LL mid = right-(right-left)/2; | ||
if (isOK(mid, chargeTimes, runningCosts, budget)) | ||
left = mid; | ||
else | ||
right = mid-1; | ||
} | ||
return left; | ||
} | ||
|
||
bool isOK(LL k, vector<int>& chargeTimes, vector<int>& runningCosts, long long budget) | ||
{ | ||
LL n = chargeTimes.size(); | ||
LL sum = 0; | ||
deque<int>dq; | ||
|
||
for (int i=0; i<n; i++) | ||
{ | ||
sum += runningCosts[i]; | ||
while (!dq.empty() && chargeTimes[dq.back()] <= chargeTimes[i]) | ||
dq.pop_back(); | ||
dq.push_back(i); | ||
while (!dq.empty() && dq.front() <= i-k) | ||
dq.pop_front(); | ||
|
||
if (i>=k-1) | ||
{ | ||
LL ret = chargeTimes[dq.front()] + (LL)k * sum; | ||
if (ret <= budget) return true; | ||
sum -= runningCosts[i-k+1]; | ||
|
||
} | ||
} | ||
|
||
return false; | ||
} | ||
}; |