forked from PRAteek-singHWY/hackoctoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggressive_cow.cpp
More file actions
37 lines (33 loc) · 817 Bytes
/
Copy pathaggressive_cow.cpp
File metadata and controls
37 lines (33 loc) · 817 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
class Solution {
public:
int solve(int n, int k, vector<int> &stalls) {
// Write your code here
int start=1,end,mid,ans;
//sort them in inc order
sort(stalls.begin(),stalls.end());
end=stalls[n-1]-stalls[0];
while(start<=end)
{
mid=start+(end-start)/2;
int count=1,pos=stalls[0];
for(int i=1;i<n;i++)
{
if(pos+mid<=stalls[i])
{
count++;
pos=stalls[i];
}
}
if(count<k)
{
end=mid-1;
}
else
{
ans=mid;
start=mid+1;
}
}
return ans;
}
};