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
| func minimumSeconds(nums []int) int { listMap := make(map[int][]int) for i, num := range nums { listMap[num] = append(listMap[num], i) } n := len(nums) ans := n for _, list := range listMap { n2 := len(list) distance := list[0] + n - list[n2-1] for i := 1; i < n2; i++ { distance = max(distance, list[i]-list[i-1]) } ans = min(ans, distance>>1) } return ans }
func max(num1, num2 int) int { if num1 >= num2 { return num1 } return num2 }
func min(num1, num2 int) int { if num1 <= num2 { return num1 } return num2 }
|