1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| func twoCitySchedCost(costs [][]int) int { sum := 0 diff := make([]int, len(costs)) for ind, val := range costs { // 所有去A地的和 sum += val[0] // 去B地与去A地的差值 diff[ind] = val[1] - val[0] }
sort.Ints(diff)
// 选一半去B地 for i := 0; i < len(costs) / 2; i++ { sum += diff[i] }
return sum }
|