classSolution: defthreeEqualParts(self, arr: List[int]) -> List[int]: s = sum(arr) if s % 3: return [-1, -1] if s == 0: return [0, 2]
partial = s // 3 first = second = third = cur = 0 for i, x inenumerate(arr): if x: if cur == 0: first = i elif cur == partial: second = i elif cur == 2 * partial: third = i cur += 1
n = len(arr) length = n - third if first + length <= second and second + length <= third: i = 0 while third + i < n: if arr[first + i] != arr[second + i] or arr[first + i] != arr[third + i]: return [-1, -1] i += 1 return [first + length - 1, second + length] return [-1, -1]
classSolution { public: vector<int> threeEqualParts(vector<int>& arr){ int sum = accumulate(arr.begin(), arr.end(), 0); if (sum % 3 != 0) { return {-1, -1}; } if (sum == 0) { return {0, 2}; }
int partial = sum / 3; int first = 0, second = 0, third = 0, cur = 0; for (int i = 0; i < arr.size(); i++) { if (arr[i] == 1) { if (cur == 0) { first = i; } elseif (cur == partial) { second = i; } elseif (cur == 2 * partial) { third = i; } cur++; } }
int len = (int)arr.size() - third; if (first + len <= second && second + len <= third) { int i = 0; while (third + i < arr.size()) { if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) { return {-1, -1}; } i++; } return {first + len - 1, second + len}; } return {-1, -1}; } };
publicclassSolution { publicint[] ThreeEqualParts(int[] arr) { int sum = arr.Sum(); if (sum % 3 != 0) { returnnewint[]{-1, -1}; } if (sum == 0) { returnnewint[]{0, 2}; }
intpartial = sum / 3; int first = 0, second = 0, third = 0, cur = 0; for (int i = 0; i < arr.Length; i++) { if (arr[i] == 1) { if (cur == 0) { first = i; } elseif (cur == partial) { second = i; } elseif (cur == 2 * partial) { third = i; } cur++; } }
int len = arr.Length - third; if (first + len <= second && second + len <= third) { int i = 0; while (third + i < arr.Length) { if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) { returnnewint[]{-1, -1}; } i++; } returnnewint[]{first + len - 1, second + len}; } returnnewint[]{-1, -1}; } }
int* threeEqualParts(int* arr, int arrSize, int* returnSize) { int sum = 0; int *ans = (int *)malloc(sizeof(int) * 2); *returnSize = 2; for (int i = 0; i < arrSize; i++) { sum += arr[i]; } if (sum % 3 != 0) { ans[0] = -1, ans[1] = -1; return ans; } if (sum == 0) { ans[0] = 0, ans[1] = 2; return ans; }
int partial = sum / 3; int first = 0, second = 0, third = 0, cur = 0; for (int i = 0; i < arrSize; i++) { if (arr[i] == 1) { if (cur == 0) { first = i; } elseif (cur == partial) { second = i; } elseif (cur == 2 * partial) { third = i; } cur++; } }
int len = (int)arrSize - third; if (first + len <= second && second + len <= third) { int i = 0; while (third + i < arrSize) { if (arr[first + i] != arr[second + i] || arr[first + i] != arr[third + i]) { ans[0] = -1, ans[1] = -1; return ans; } i++; } ans[0] = first + len - 1, ans[1] = second + len; return ans; } ans[0] = -1, ans[1] = -1; return ans; }
var threeEqualParts = function(arr) { const sum = _.sum(arr); if (sum % 3 !== 0) { return [-1, -1]; } if (sum === 0) { return [0, 2]; }
const partial = Math.floor(sum / 3); let first = 0, second = 0, third = 0, cur = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] === 1) { if (cur === 0) { first = i; } elseif (cur === partial) { second = i; } elseif (cur === 2 * partial) { third = i; } cur++; } }
let len = arr.length - third; if (first + len <= second && second + len <= third) { let i = 0; while (third + i < arr.length) { if (arr[first + i] !== arr[second + i] || arr[first + i] !== arr[third + i]) { return [-1, -1]; } i++; } return [first + len - 1, second + len]; } return [-1, -1]; };
functhreeEqualParts(arr []int) []int { sum := 0 for _, v := range arr { sum += v } if sum%3 != 0 { return []int{-1, -1} } if sum == 0 { return []int{0, 2} }
partial := sum / 3 first, second, third, cur := 0, 0, 0, 0 for i, x := range arr { if x == 1 { if cur == 0 { first = i } elseif cur == partial { second = i } elseif cur == 2*partial { third = i } cur++ } }
n := len(arr) length := n - third if first+length <= second && second+length <= third { i := 0 for third+i < n { if arr[first+i] != arr[second+i] || arr[first+i] != arr[third+i] { return []int{-1, -1} } i++ } return []int{first + length - 1, second + length} } return []int{-1, -1} }
复杂度分析
时间复杂度:O(n),其中 n 是 arr 的长度。找到三个下标的时间复杂度为 O(n),判断三个部分是否相同的时间复杂度也是 O(n)。