diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README.md b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README.md index 03701c1b6bc04..91810a4519b3d 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README.md +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README.md @@ -63,13 +63,17 @@ tags: -### 方法一:双指针 +### 方法一:遍历求和 -先遍历数组 `arr`,得到数组所有元素的和,记为 `s`。如果 `s` 不能被 3 整除,那么数组 `arr` 不能被分成和相等的三个部分,直接返回 `false`。 +我们先求出整个数组的和,然后判断和是否能被 3 整除,如果不能,直接返回 $\textit{false}$。 -接下来,利用双指针 `i`, `j` 找三等分和的边界,若成功找到,返回 `true`,否则返回 `false`。 +否则,我们记 $\textit{s}$ 表示每部分的和,用一个变量 $\textit{cnt}$ 记录当前已经找到的部分数,另一个变量 $\textit{t}$ 记录当前部分的和。初始时 $\textit{cnt} = 0$, $t = 0$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 为数组 `arr` 的长度。 +然后我们遍历数组,对于每个元素 $x$,我们将 $t$ 加上 $x$,如果 $t$ 等于 $s$,说明找到了一部分,将 $\textit{cnt}$ 加一,然后将 $t$ 置为 0。 + +最后判断 $\textit{cnt}$ 是否大于等于 3 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -78,22 +82,16 @@ tags: ```python class Solution: def canThreePartsEqualSum(self, arr: List[int]) -> bool: - s = sum(arr) - if s % 3 != 0: + s, mod = divmod(sum(arr), 3) + if mod: return False - i, j = 0, len(arr) - 1 - a = b = 0 - while i < len(arr): - a += arr[i] - if a == s // 3: - break - i += 1 - while ~j: - b += arr[j] - if b == s // 3: - break - j -= 1 - return i < j - 1 + cnt = t = 0 + for x in arr: + t += x + if t == s: + cnt += 1 + t = 0 + return cnt >= 3 ``` #### Java @@ -101,30 +99,20 @@ class Solution: ```java class Solution { public boolean canThreePartsEqualSum(int[] arr) { - int s = 0; - for (int v : arr) { - s += v; - } + int s = Arrays.stream(arr).sum(); if (s % 3 != 0) { return false; } - int i = 0, j = arr.length - 1; - int a = 0, b = 0; - while (i < arr.length) { - a += arr[i]; - if (a == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + cnt++; + t = 0; } - ++i; } - while (j >= 0) { - b += arr[j]; - if (b == s / 3) { - break; - } - --j; - } - return i < j - 1; + return cnt >= 3; } } ``` @@ -135,26 +123,20 @@ class Solution { class Solution { public: bool canThreePartsEqualSum(vector& arr) { - int s = 0; - for (int v : arr) s += v; - if (s % 3) return false; - int i = 0, j = arr.size() - 1; - int a = 0, b = 0; - while (i < arr.size()) { - a += arr[i]; - if (a == s / 3) { - break; - } - ++i; + int s = accumulate(arr.begin(), arr.end(), 0); + if (s % 3) { + return false; } - while (~j) { - b += arr[j]; - if (b == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + t = 0; + cnt++; } - --j; } - return i < j - 1; + return cnt >= 3; } }; ``` @@ -164,29 +146,70 @@ public: ```go func canThreePartsEqualSum(arr []int) bool { s := 0 - for _, v := range arr { - s += v + for _, x := range arr { + s += x } if s%3 != 0 { return false } - i, j := 0, len(arr)-1 - a, b := 0, 0 - for i < len(arr) { - a += arr[i] - if a == s/3 { - break + s /= 3 + cnt, t := 0, 0 + for _, x := range arr { + t += x + if t == s { + cnt++ + t = 0 } - i++ } - for j >= 0 { - b += arr[j] - if b == s/3 { - break - } - j-- - } - return i < j-1 + return cnt >= 3 +} +``` + +#### TypeScript + +```ts +function canThreePartsEqualSum(arr: number[]): boolean { + let s = arr.reduce((a, b) => a + b); + if (s % 3) { + return false; + } + s = (s / 3) | 0; + let [cnt, t] = [0, 0]; + for (const x of arr) { + t += x; + if (t == s) { + cnt++; + t = 0; + } + } + return cnt >= 3; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn can_three_parts_equal_sum(arr: Vec) -> bool { + let sum: i32 = arr.iter().sum(); + let s = sum / 3; + let mod_val = sum % 3; + if mod_val != 0 { + return false; + } + + let mut cnt = 0; + let mut t = 0; + for &x in &arr { + t += x; + if t == s { + cnt += 1; + t = 0; + } + } + + cnt >= 3 + } } ``` diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README_EN.md b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README_EN.md index 11d0de73f21c4..67785c36c8754 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README_EN.md +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/README_EN.md @@ -61,7 +61,17 @@ tags: -### Solution 1 +### Solution 1: Traversal and Summation + +First, we calculate the sum of the entire array and check if the sum is divisible by 3. If it is not, we directly return $\textit{false}$. + +Otherwise, let $\textit{s}$ represent the sum of each part. We use a variable $\textit{cnt}$ to record the number of parts found so far, and another variable $\textit{t}$ to record the current part's sum. Initially, $\textit{cnt} = 0$ and $\textit{t} = 0$. + +Then we traverse the array. For each element $x$, we add $x$ to $\textit{t}$. If $\textit{t}$ equals $s$, it means we have found one part, so we increment $\textit{cnt}$ by one and reset $\textit{t}$ to 0. + +Finally, we check if $\textit{cnt}$ is greater than or equal to 3. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$. @@ -70,22 +80,16 @@ tags: ```python class Solution: def canThreePartsEqualSum(self, arr: List[int]) -> bool: - s = sum(arr) - if s % 3 != 0: + s, mod = divmod(sum(arr), 3) + if mod: return False - i, j = 0, len(arr) - 1 - a = b = 0 - while i < len(arr): - a += arr[i] - if a == s // 3: - break - i += 1 - while ~j: - b += arr[j] - if b == s // 3: - break - j -= 1 - return i < j - 1 + cnt = t = 0 + for x in arr: + t += x + if t == s: + cnt += 1 + t = 0 + return cnt >= 3 ``` #### Java @@ -93,30 +97,20 @@ class Solution: ```java class Solution { public boolean canThreePartsEqualSum(int[] arr) { - int s = 0; - for (int v : arr) { - s += v; - } + int s = Arrays.stream(arr).sum(); if (s % 3 != 0) { return false; } - int i = 0, j = arr.length - 1; - int a = 0, b = 0; - while (i < arr.length) { - a += arr[i]; - if (a == s / 3) { - break; - } - ++i; - } - while (j >= 0) { - b += arr[j]; - if (b == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + cnt++; + t = 0; } - --j; } - return i < j - 1; + return cnt >= 3; } } ``` @@ -127,26 +121,20 @@ class Solution { class Solution { public: bool canThreePartsEqualSum(vector& arr) { - int s = 0; - for (int v : arr) s += v; - if (s % 3) return false; - int i = 0, j = arr.size() - 1; - int a = 0, b = 0; - while (i < arr.size()) { - a += arr[i]; - if (a == s / 3) { - break; - } - ++i; + int s = accumulate(arr.begin(), arr.end(), 0); + if (s % 3) { + return false; } - while (~j) { - b += arr[j]; - if (b == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + t = 0; + cnt++; } - --j; } - return i < j - 1; + return cnt >= 3; } }; ``` @@ -156,29 +144,70 @@ public: ```go func canThreePartsEqualSum(arr []int) bool { s := 0 - for _, v := range arr { - s += v + for _, x := range arr { + s += x } if s%3 != 0 { return false } - i, j := 0, len(arr)-1 - a, b := 0, 0 - for i < len(arr) { - a += arr[i] - if a == s/3 { - break - } - i++ - } - for j >= 0 { - b += arr[j] - if b == s/3 { - break + s /= 3 + cnt, t := 0, 0 + for _, x := range arr { + t += x + if t == s { + cnt++ + t = 0 } - j-- } - return i < j-1 + return cnt >= 3 +} +``` + +#### TypeScript + +```ts +function canThreePartsEqualSum(arr: number[]): boolean { + let s = arr.reduce((a, b) => a + b); + if (s % 3) { + return false; + } + s = (s / 3) | 0; + let [cnt, t] = [0, 0]; + for (const x of arr) { + t += x; + if (t == s) { + cnt++; + t = 0; + } + } + return cnt >= 3; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn can_three_parts_equal_sum(arr: Vec) -> bool { + let sum: i32 = arr.iter().sum(); + let s = sum / 3; + let mod_val = sum % 3; + if mod_val != 0 { + return false; + } + + let mut cnt = 0; + let mut t = 0; + for &x in &arr { + t += x; + if t == s { + cnt += 1; + t = 0; + } + } + + cnt >= 3 + } } ``` diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.cpp b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.cpp index 588ad9f638df8..f744ceee37ef6 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.cpp +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.cpp @@ -1,25 +1,19 @@ class Solution { public: bool canThreePartsEqualSum(vector& arr) { - int s = 0; - for (int v : arr) s += v; - if (s % 3) return false; - int i = 0, j = arr.size() - 1; - int a = 0, b = 0; - while (i < arr.size()) { - a += arr[i]; - if (a == s / 3) { - break; - } - ++i; + int s = accumulate(arr.begin(), arr.end(), 0); + if (s % 3) { + return false; } - while (~j) { - b += arr[j]; - if (b == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + t = 0; + cnt++; } - --j; } - return i < j - 1; + return cnt >= 3; } -}; \ No newline at end of file +}; diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.go b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.go index 654d9b63e4081..269b01e2cfece 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.go +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.go @@ -1,26 +1,19 @@ func canThreePartsEqualSum(arr []int) bool { s := 0 - for _, v := range arr { - s += v + for _, x := range arr { + s += x } if s%3 != 0 { return false } - i, j := 0, len(arr)-1 - a, b := 0, 0 - for i < len(arr) { - a += arr[i] - if a == s/3 { - break + s /= 3 + cnt, t := 0, 0 + for _, x := range arr { + t += x + if t == s { + cnt++ + t = 0 } - i++ } - for j >= 0 { - b += arr[j] - if b == s/3 { - break - } - j-- - } - return i < j-1 -} \ No newline at end of file + return cnt >= 3 +} diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.java b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.java index be43a71a1c781..777028246a6b0 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.java +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.java @@ -1,28 +1,18 @@ class Solution { public boolean canThreePartsEqualSum(int[] arr) { - int s = 0; - for (int v : arr) { - s += v; - } + int s = Arrays.stream(arr).sum(); if (s % 3 != 0) { return false; } - int i = 0, j = arr.length - 1; - int a = 0, b = 0; - while (i < arr.length) { - a += arr[i]; - if (a == s / 3) { - break; - } - ++i; - } - while (j >= 0) { - b += arr[j]; - if (b == s / 3) { - break; + s /= 3; + int cnt = 0, t = 0; + for (int x : arr) { + t += x; + if (t == s) { + cnt++; + t = 0; } - --j; } - return i < j - 1; + return cnt >= 3; } -} \ No newline at end of file +} diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.py b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.py index e976ffdf3cc35..841e3bfa81aa2 100644 --- a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.py +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.py @@ -1,18 +1,12 @@ class Solution: def canThreePartsEqualSum(self, arr: List[int]) -> bool: - s = sum(arr) - if s % 3 != 0: + s, mod = divmod(sum(arr), 3) + if mod: return False - i, j = 0, len(arr) - 1 - a = b = 0 - while i < len(arr): - a += arr[i] - if a == s // 3: - break - i += 1 - while ~j: - b += arr[j] - if b == s // 3: - break - j -= 1 - return i < j - 1 + cnt = t = 0 + for x in arr: + t += x + if t == s: + cnt += 1 + t = 0 + return cnt >= 3 diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.rs b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.rs new file mode 100644 index 0000000000000..1504875ff513d --- /dev/null +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn can_three_parts_equal_sum(arr: Vec) -> bool { + let sum: i32 = arr.iter().sum(); + let s = sum / 3; + let mod_val = sum % 3; + if mod_val != 0 { + return false; + } + + let mut cnt = 0; + let mut t = 0; + for &x in &arr { + t += x; + if t == s { + cnt += 1; + t = 0; + } + } + + cnt >= 3 + } +} diff --git a/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.ts b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.ts new file mode 100644 index 0000000000000..f2a7393352898 --- /dev/null +++ b/solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum/Solution.ts @@ -0,0 +1,16 @@ +function canThreePartsEqualSum(arr: number[]): boolean { + let s = arr.reduce((a, b) => a + b); + if (s % 3) { + return false; + } + s = (s / 3) | 0; + let [cnt, t] = [0, 0]; + for (const x of arr) { + t += x; + if (t == s) { + cnt++; + t = 0; + } + } + return cnt >= 3; +}