We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 2a12d0c + 69c1b88 commit 29596f8Copy full SHA for 29596f8
Aggressive Cows
@@ -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