funcsplitArray(nums []int, m int)int { left, right := max(nums...), sum(nums...)
for left <= right { mid := left + (right - left) / 2 midValue := split(nums, mid) if midValue < m { right = mid - 1 } elseif midValue > m { left = mid + 1 } else { right = mid - 1 } } return left }
// 最大值为 max 时能分出的数组个数为 split(nums, max) funcsplit(nums []int, max int)int { count := 0 remains := 0 for i := 0; i < len(nums); i++ { if remains - nums[i] < 0 { remains = max count++ } remains -= nums[i] } return count }
funcmax(values ...int)int { res := values[0] for _, v := range values { if v > res { res = v } } return res }
funcsum(values ...int)int { res := 0 for _, v := range values { res += v } return res }