-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathAggressiveCow.java
More file actions
43 lines (42 loc) · 1.11 KB
/
AggressiveCow.java
File metadata and controls
43 lines (42 loc) · 1.11 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
public class AggressiveCow {
public static void main(String args[])
{
int[] stalls={1,2,4,8,9};
int cows=3;
int result=aggressiveCows(stalls,cows);
System.out.println("The largest minimum distance is : "+result);
}
public static int aggressiveCows(int[] stalls,int cows)
{
int left=1;
int right=stalls[stalls.length-1]-stalls[0];
int answer=-1;
while(left<=right)
{
int mid=left+(right-left)/2;
if(canPlaceCows(stalls,cows,mid))
{
answer=mid;
left=mid+1;
}
else{
right=mid-1;
}
}
return answer;
}
public static boolean canPlaceCows(int[] stalls,int cows,int distance)
{
int countCows=1;
int lastPosition=stalls[0];
for(int i=1;i<stalls.length;i++)
{
if(stalls[i]-lastPosition>=distance)
{
countCows++;
lastPosition=stalls[i];
}
}
return countCows>=cows;
}
}