Skip to content
This repository was archived by the owner on Oct 5, 2025. It is now read-only.
This repository was archived by the owner on Oct 5, 2025. It is now read-only.

</> Aggressive Cows ! Well this helps! (Python(3.10)) #452

@anudeepkantamaneni

Description

@anudeepkantamaneni

def aggressiveCows(stalls: list[int], k: int) -> int:
stalls.sort()
low = 1
high = stalls[-1] - stalls[0]
result = 0

def can_place_cows(distance: int) -> bool:
    count = 1
    last_pos = stalls[0]
    for i in range(1, len(stalls)):
        if stalls[i] - last_pos >= distance:
            count += 1
            last_pos = stalls[i]
            if count == k:
                return True
    return False

while low <= high:
    mid = (low + high) // 2
    if can_place_cows(mid):
        result = mid
        low = mid + 1
    else:
        high = mid - 1

return result

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions