-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggressiveCows.cpp
More file actions
35 lines (33 loc) · 875 Bytes
/
Copy pathAggressiveCows.cpp
File metadata and controls
35 lines (33 loc) · 875 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
class Solution {
public:
bool canPlace(vector<int>&stalls,int distance,int k) {
int last_position = stalls[0];
int count = 1;
int n = stalls.size();
for (int i = 1;i<n;i++) {
if(stalls[i]-last_position>=distance) {
count ++;
last_position = stalls[i];
}
if (count>=k) return true;
}
return false;
}
int aggressiveCows(vector<int>&stalls,int k ){
sort(stalls.begin(),stalls.end());
int n = stalls.size();
int low = 1;
int high = stalls[n-1]-stalls[0];
int ans = 1;
while (low<=high) {
int mid = low +(high-low)/2;
if(canPlace(stalls,mid,k)) {
ans = mid;
low = mid+1;
}else {
high = mid-1;
}
}
return ans;
}
};