Leetcode Note: Go - Distance Between Bus Stops
Distance Between Bus Stops - LeetCode
https://leetcode.com/problems/distance-between-bus-stops/
- Go 言語で取り組んだメモ
回答
Golang Solution. Runtime: 0ms Memory: 2.8Mb - LeetCode Discuss
https://leetcode.com/problems/distance-between-bus-stops/discuss/378098/Golang-Solution.-Runtime%3A-0ms-Memory%3A-2.8Mb
func distanceBetweenBusStops(distance []int, start int, destination int) int {
if len(distance) == 0 || start == destination {
return 0
}
if start > destination {
tmp := start
start = destination
destination = tmp
}
clockwise := sum(distance[start:destination])
counterClockwise := sum(distance) - clockwise
if clockwise < counterClockwise {
return clockwise
}
return counterClockwise
}
func sum(ar []int) int {
var sum int
for _, v := range ar {
sum += v
}
return sum
}