-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggresiveCow.cpp
More file actions
47 lines (45 loc) · 1.2 KB
/
Copy pathaggresiveCow.cpp
File metadata and controls
47 lines (45 loc) · 1.2 KB
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
40
41
42
43
44
45
46
47
//Aggresive Cow Problem.
//You're given N stalls and you need to place C cows such that the minimum distance between any two cows is as large as possible.
//You return this maximum of the minimum distances.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isValid(vector<int> arr,int N, int C, int longMinDis){
int cow=1,currCowPosition=arr[0];
for (int i=1; i<N ; i++ ){
if(arr[i]-currCowPosition>=longMinDis){
currCowPosition=arr[i];
cow++;
}
if(cow==C){
return true;
}
}
return false;
}
int binarySearch(vector<int> arr, int N, int C){ //O(log(n) * N)
if (C>N){
return -1;
}
int ans=-1;
int start=1;
sort(arr.begin(),arr.end());
int end=arr[N-1]-arr[0];
while(start<=end){
int mid=start + (end-start)/2;
if (isValid(arr,N,C,mid)){
ans=mid;
start=mid+1;
}else{
end=mid-1;
}
}
return ans;
}
int main(){
vector<int> arr={4,3,9,7,6};
int N=5, C=3;
int answer=binarySearch(arr,N,C);
cout<<"Longest minimum distance: "<<answer;
}