Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-113

Aggressive Cows

Problem

Given stall positions and a number of cows, return the maximum possible minimum distance between any two cows when all cows are placed in distinct stalls. Return -1 when cows < 2 or cows > len(stalls).

Binary search the distance and greedily place a cow whenever the gap from the last placed stall is at least the candidate.

Hint

The upper bound for the gap is the distance between the first and last stall.

Test cases

case 1
in: [[1,2,8,4,9],3]
expected: 3
case 2
in: [[1,2,4,8,9],3]
expected: 3
case 3
in: [[1,2,8,4,9],4]
expected: 1
case 4
in: [[1,5],2]
expected: 4
case 5
in: [[1,2,3,4,5],2]
expected: 4
Starter codePython
def aggressive_cows(stalls, cows):
    # Your code here
    pass

5 test cases · run your code against them in the editor