Skip to content

Commit 29596f8

Browse files
authored
Merge pull request #8 from Chayandas07/Chayandas07-patch-8
Create Aggressive Cows
2 parents 2a12d0c + 69c1b88 commit 29596f8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Aggressive Cows

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
4+
int solve(int n, int k, vector<int> &stalls) {
5+
sort(stalls.begin(),stalls.end());
6+
int l=0,r=stalls[n-1]-stalls[0];
7+
int ans=0;
8+
while(l<=r){
9+
int mid=(l+r)/2;
10+
int cow=1;
11+
int last=stalls[0];
12+
for(int i=1;i<n;i++){
13+
if(stalls[i]-last>=mid){
14+
cow++;
15+
last=stalls[i];
16+
}
17+
}
18+
if(cow>=k){
19+
ans=mid;
20+
l=mid+1;
21+
}
22+
else{
23+
r=mid-1;
24+
}
25+
}
26+
return ans;
27+
}
28+
};

0 commit comments

Comments
 (0)