-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggressive_Cows.cpp
More file actions
39 lines (34 loc) · 925 Bytes
/
Copy pathAggressive_Cows.cpp
File metadata and controls
39 lines (34 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// User function Template for C++
class Solution {
public:
bool isPossible(vector<int> &stalls, int n, int c, int mid){
int cows=1, lastStallPos = stalls[0];
for(int i=1;i<n;i++) {
if((stalls[i]-lastStallPos) >= mid){
cows++;
lastStallPos=stalls[i];
}
if (cows==c){
return true;
}
}
return false;
}
int aggressiveCows(vector<int> &stalls, int k) {
int n = stalls.size();
int ans=-1;
sort(stalls.begin(),stalls.end());
int st=0,end=stalls[n-1]-stalls[0];
while(st<=end){
int mid= (end+st)>>1;
if(isPossible(stalls, n, k, mid)){
ans=mid;
st=mid+1;
}
else {
end=mid-1;
}
}
return ans;
}
};