classSolution { publicintsplitArray(int[] nums, int m) { intn= nums.length; int[][] f = newint[n + 1][m + 1]; for (inti=0; i <= n; i++) { Arrays.fill(f[i], Integer.MAX_VALUE); } int[] sub = newint[n + 1]; for (inti=0; i < n; i++) { sub[i + 1] = sub[i] + nums[i]; } f[0][0] = 0; for (inti=1; i <= n; i++) { for (intj=1; j <= Math.min(i, m); j++) { for (intk=0; k < i; k++) { f[i][j] = Math.min(f[i][j], Math.max(f[k][j - 1], sub[i] - sub[k])); } } } return f[n][m]; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defsplitArray(self, nums: List[int], m: int) -> int: n = len(nums) f = [[10**18] * (m + 1) for _ inrange(n + 1)] sub = [0] for elem in nums: sub.append(sub[-1] + elem) f[0][0] = 0 for i inrange(1, n + 1): for j inrange(1, min(i, m) + 1): for k inrange(i): f[i][j] = min(f[i][j], max(f[k][j - 1], sub[i] - sub[k])) return f[n][m]
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intsplitArray(int* nums, int numsSize, int m) { longlong f[numsSize + 1][m + 1]; memset(f, 0x3f, sizeof(f)); longlong sub[numsSize + 1]; memset(sub, 0, sizeof(sub)); for (int i = 0; i < numsSize; i++) { sub[i + 1] = sub[i] + nums[i]; } f[0][0] = 0; for (int i = 1; i <= numsSize; i++) { for (int j = 1; j <= fmin(i, m); j++) { for (int k = 0; k < i; k++) { f[i][j] = fmin(f[i][j], fmax(f[k][j - 1], sub[i] - sub[k])); } } } return (int)f[numsSize][m]; }
classSolution { public: boolcheck(vector<int>& nums, int x, int m){ longlong sum = 0; int cnt = 1; for (int i = 0; i < nums.size(); i++) { if (sum + nums[i] > x) { cnt++; sum = nums[i]; } else { sum += nums[i]; } } return cnt <= m; }
intsplitArray(vector<int>& nums, int m){ longlong left = 0, right = 0; for (int i = 0; i < nums.size(); i++) { right += nums[i]; if (left < nums[i]) { left = nums[i]; } } while (left < right) { longlong mid = (left + right) >> 1; if (check(nums, mid, m)) { right = mid; } else { left = mid + 1; } } return left; } };
classSolution { publicintsplitArray(int[] nums, int m) { intleft=0, right = 0; for (inti=0; i < nums.length; i++) { right += nums[i]; if (left < nums[i]) { left = nums[i]; } } while (left < right) { intmid= (right - left) / 2 + left; if (check(nums, mid, m)) { right = mid; } else { left = mid + 1; } } return left; }
publicbooleancheck(int[] nums, int x, int m) { intsum=0; intcnt=1; for (inti=0; i < nums.length; i++) { if (sum + nums[i] > x) { cnt++; sum = nums[i]; } else { sum += nums[i]; } } return cnt <= m; } }
classSolution: defsplitArray(self, nums: List[int], m: int) -> int: defcheck(x: int) -> bool: total, cnt = 0, 1 for num in nums: if total + num > x: cnt += 1 total = num else: total += num return cnt <= m
left = max(nums) right = sum(nums) while left < right: mid = (left + right) // 2 if check(mid): right = mid else: left = mid + 1
boolcheck(int* nums, int numsSize, int m, int x) { longlong sum = 0; int cnt = 1; for (int i = 0; i < numsSize; i++) { if (sum + nums[i] > x) { cnt++; sum = nums[i]; } else { sum += nums[i]; } } return cnt <= m; }
intsplitArray(int* nums, int numsSize, int m) { longlong left = 0, right = 0; for (int i = 0; i < numsSize; i++) { right += nums[i]; if (left < nums[i]) { left = nums[i]; } } while (left < right) { longlong mid = (left + right) >> 1; if (check(nums, numsSize, m, mid)) { right = mid; } else { left = mid + 1; } } return left; }
funcsplitArray(nums []int, m int)int { left, right := 0, 0 for i := 0; i < len(nums); i++ { right += nums[i] if left < nums[i] { left = nums[i] } } for left < right { mid := (right - left) / 2 + left if check(nums, mid, m) { right = mid } else { left = mid + 1 } } return left }
funccheck(nums []int, x, m int)bool { sum, cnt := 0, 1 for i := 0; i < len(nums); i++ { if sum + nums[i] > x { cnt++ sum = nums[i] } else { sum += nums[i] } } return cnt <= m }