-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_distance.py
49 lines (47 loc) · 1.25 KB
/
max_distance.py
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
44
45
46
47
48
49
class Solution:
def maxDistance(self, position, m):
"""
:type position: List[int]
:type m: int
:rtype: int
"""
position.sort()
lo = 1
hi = (position[-1] - position[0]) // (m - 1)
ans = 1
while lo <= hi:
mid = (hi + lo) // 2
print(mid)
if self.canWePlace(position, mid, m):
ans = mid
lo = mid + 1
print("in if")
print(mid)
print(hi)
else:
hi = mid - 1
print("in else")
print(mid)
return ans
def canWePlace(self, arr, dist, cows):
"""
:type arr: List[int]
:type dist: int
:type cows: int
:rtype: bool
"""
cntCows = 1
last = arr[0]
for i in range(1, len(arr)): # Start loop from 1 instead of 0
if arr[i] - last >= dist:
cntCows += 1
last = arr[i]
if cntCows >= cows:
return True
return False
# Example usage:
solution = Solution()
position = [5, 4, 3, 2, 1, 1000000000]
m = 2
result = solution.maxDistance(position, m)
print(result) # Outputs the result