Skip to content

Commit 9c3b0d4

Browse files
committed
Force
1 parent 0e94e64 commit 9c3b0d4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

June20/Force.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import "sort"
4+
5+
func maxDistance(position []int, m int) int {
6+
sort.Ints(position)
7+
low := 1
8+
high := (position[len(position)-1] - position[0]) / (m - 1)
9+
answer := 1
10+
for low <= high {
11+
mid := low + (high - low) / 2
12+
if CanWePlace(position, mid, m) {
13+
answer = mid
14+
low = mid + 1
15+
} else {
16+
high = mid - 1
17+
}
18+
}
19+
return answer
20+
}
21+
22+
func CanWePlace(arr []int, dist, cow int) bool {
23+
cntCows := 1
24+
last := arr[0]
25+
for i := 1; i < len(arr); i++ {
26+
if arr[i] - last >= dist {
27+
cntCows++
28+
last = arr[i]
29+
}
30+
if cntCows >= cow {
31+
return true
32+
}
33+
}
34+
return false
35+
}

0 commit comments

Comments
 (0)