问题:给定一个正的不同整数数组表示 穿越 n 人的时间。这些 n 人站在一边 桥梁桥一次最多可以容纳两个人。当两个 人们越过桥梁,他们必须以较慢的人的速度移动。 找出所有人过桥的最短时间。 person: person_1: 2 person_2: 1 person_3: 5 person_4: 8 person_5: 9
问题:给定一个正的不同整数数组表示 穿越 n 人的时间。这些 n 人站在一边 桥梁桥一次最多可以容纳两个人。当两个 人们越过桥梁,他们必须以较慢的人的速度移动。 找出所有人过桥的最短时间。
person: person_1: 2 person_2: 1 person_3: 5 person_4: 8 person_5: 9
你的算法很复杂。
例如,
package main import ( "fmt" "sort" ) func minCrossingTime(people []int) int { sort.Slice(people, func(i, j int) bool { return people[i] > people[j] }) min := 0 for i := 0; i < len(people); i += 2 { min += people[i] } return min } func main() { people := []int{2, 1, 5, 8, 9} fmt.Println(len(people), people) crossingTime := minCrossingTime(people) fmt.Println(len(people), people) fmt.Println(crossingTime) }
操场: https://play.golang.org/p/pXdGcinwxr-
输出:
5 [2 1 5 8 9] 5 [9 8 5 2 1] 15