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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| func getSortMinSub(arr []int) int { if arr == nil || len(arr) < 2 { return 0 } const ( INT_MAX = int(^uint32(0) >> 1) INT_MIN = ^INT_MAX ) max, min := INT_MIN, INT_MAX for i := 0; i < len(arr); i++ { if arr[i] > max { max = arr[i] } if arr[i] < min { min = arr[i] } } maxBarrel, minBarrel := make([]int, len(arr) + 1), make([]int, len(arr) + 1) isHaveNum := make([]bool, len(arr) + 1) for i := 0; i < len(arr); i++ { index := getBarrelNum(arr[i], max, min, len(arr)) if !isHaveNum[index] { isHaveNum[index] = true maxBarrel[index] = arr[i] minBarrel[index] = arr[i] } else { if maxBarrel[index] < arr[i] { maxBarrel[index] = arr[i] } if minBarrel[index] > arr[i] { minBarrel[index] = arr[i] } } } res := INT_MIN lastMaxValue := maxBarrel[0] for i := 1; i < len(isHaveNum); i++ { if isHaveNum[i] { tmp := minBarrel[i] - lastMaxValue if tmp > res { res = tmp } lastMaxValue = maxBarrel[i] } } return res }
func getBarrelNum(num, max, min, len int) int { return int((num - min) * len / (max - min)) }
|