Skip to content

Commit 656bef1

Browse files
committed
feat: add solutions to lc problem: No.0927
No.0927.Three Equal Parts
1 parent 4fd68f2 commit 656bef1

File tree

6 files changed

+421
-2
lines changed

6 files changed

+421
-2
lines changed

solution/0900-0999/0927.Three Equal Parts/README.md

+150-1
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,171 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:将 1 的数量三等分**
61+
62+
将 $1$ 的数量三等分,找到每一部分的第一个 $1$,分别记为 $i$, $j$, $k$。
63+
64+
然后从 $i$, $j$, $k$ 开始往后同时遍历每一部分,判断三部分对应的值是否相等,是则继续遍历,直至 $k$ 到达 $arr$ 末尾。
65+
66+
遍历结束时,若 $k=n$,说明满足三等分,返回此时的 $[i-1,j]$ 作为答案。
67+
68+
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
69+
6070
<!-- tabs:start -->
6171

6272
### **Python3**
6373

6474
<!-- 这里可写当前语言的特殊实现逻辑 -->
6575

6676
```python
67-
77+
class Solution:
78+
def threeEqualParts(self, arr: List[int]) -> List[int]:
79+
def find(cnt):
80+
s = 0
81+
for i, v in enumerate(arr):
82+
s += v
83+
if s == cnt:
84+
return i
85+
return -1
86+
87+
n = len(arr)
88+
cnt, mod = divmod(sum(arr), 3)
89+
if mod:
90+
return [-1, -1]
91+
if cnt == 0:
92+
return [0, n - 1]
93+
i = find(1)
94+
j = find(cnt + 1)
95+
k = find(cnt * 2 + 1)
96+
while k < n and arr[i] == arr[j] == arr[k]:
97+
i, j, k = i + 1, j + 1, k + 1
98+
if k == n:
99+
return [i - 1, j]
100+
return [-1, -1]
68101
```
69102

70103
### **Java**
71104

72105
<!-- 这里可写当前语言的特殊实现逻辑 -->
73106

74107
```java
108+
class Solution {
109+
public int[] threeEqualParts(int[] arr) {
110+
int n = arr.length;
111+
int cnt1 = 0;
112+
for (int v : arr) {
113+
cnt1 += v;
114+
}
115+
int cnt = cnt1 / 3;
116+
int mod = cnt1 % 3;
117+
if (mod != 0) {
118+
return new int[]{-1, -1};
119+
}
120+
if (cnt == 0) {
121+
return new int[]{0, n - 1};
122+
}
123+
int i = find(arr, 1);
124+
int j = find(arr, cnt + 1);
125+
int k = find(arr, cnt * 2 + 1);
126+
while (k < n && arr[i] == arr[j] && arr[j] == arr[k]) {
127+
++i;
128+
++j;
129+
++k;
130+
}
131+
if (k == n) {
132+
return new int[]{i - 1, j};
133+
}
134+
return new int[]{-1, -1};
135+
}
136+
137+
private int find(int[] arr, int cnt) {
138+
int s = 0;
139+
for (int i = 0; i < arr.length; ++i) {
140+
s += arr[i];
141+
if (s == cnt) {
142+
return i;
143+
}
144+
}
145+
return -1;
146+
}
147+
}
148+
```
149+
150+
### **C++**
151+
152+
```cpp
153+
class Solution {
154+
public:
155+
vector<int> threeEqualParts(vector<int>& arr) {
156+
int n = arr.size();
157+
int cnt1 = accumulate(arr.begin(), arr.end(), 0);
158+
int cnt = cnt1 / 3;
159+
int mod = cnt1 % 3;
160+
if (mod) return {-1, -1};
161+
if (cnt == 0) return {0, n - 1};
162+
int i = find(arr, 1);
163+
int j = find(arr, cnt + 1);
164+
int k = find(arr, cnt * 2 + 1);
165+
while (k < n && arr[i] == arr[j] && arr[j] == arr[k])
166+
{
167+
++i;
168+
++j;
169+
++k;
170+
}
171+
if (k == n) return {i - 1, j};
172+
return {-1, -1};
173+
}
174+
175+
int find(vector<int>& arr, int cnt) {
176+
int s = 0;
177+
for (int i = 0; i < arr.size(); ++i)
178+
{
179+
s += arr[i];
180+
if (s == cnt) return i;
181+
}
182+
return -1;
183+
}
184+
};
185+
```
75186

187+
### **Go**
188+
189+
```go
190+
func threeEqualParts(arr []int) []int {
191+
n := len(arr)
192+
cnt1 := 0
193+
for _, v := range arr {
194+
cnt1 += v
195+
}
196+
cnt := cnt1 / 3
197+
mod := cnt1 % 3
198+
if mod != 0 {
199+
return []int{-1, -1}
200+
}
201+
if cnt == 0 {
202+
return []int{0, n - 1}
203+
}
204+
find := func(cnt int) int {
205+
s := 0
206+
for i, v := range arr {
207+
s += v
208+
if s == cnt {
209+
return i
210+
}
211+
}
212+
return -1
213+
}
214+
i, j, k := find(1), find(cnt+1), find(cnt*2+1)
215+
for k < n && arr[i] == arr[j] && arr[j] == arr[k] {
216+
i++
217+
j++
218+
k++
219+
}
220+
if k == n {
221+
return []int{i - 1, j}
222+
}
223+
return []int{-1, -1}
224+
}
76225
```
77226

78227
### **...**

solution/0900-0999/0927.Three Equal Parts/README_EN.md

+140-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,152 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def threeEqualParts(self, arr: List[int]) -> List[int]:
50+
def find(cnt):
51+
s = 0
52+
for i, v in enumerate(arr):
53+
s += v
54+
if s == cnt:
55+
return i
56+
return -1
57+
58+
n = len(arr)
59+
cnt, mod = divmod(sum(arr), 3)
60+
if mod:
61+
return [-1, -1]
62+
if cnt == 0:
63+
return [0, n - 1]
64+
i = find(1)
65+
j = find(cnt + 1)
66+
k = find(cnt * 2 + 1)
67+
while k < n and arr[i] == arr[j] == arr[k]:
68+
i, j, k = i + 1, j + 1, k + 1
69+
if k == n:
70+
return [i - 1, j]
71+
return [-1, -1]
4972
```
5073

5174
### **Java**
5275

5376
```java
77+
class Solution {
78+
public int[] threeEqualParts(int[] arr) {
79+
int n = arr.length;
80+
int cnt1 = 0;
81+
for (int v : arr) {
82+
cnt1 += v;
83+
}
84+
int cnt = cnt1 / 3;
85+
int mod = cnt1 % 3;
86+
if (mod != 0) {
87+
return new int[]{-1, -1};
88+
}
89+
if (cnt == 0) {
90+
return new int[]{0, n - 1};
91+
}
92+
int i = find(arr, 1);
93+
int j = find(arr, cnt + 1);
94+
int k = find(arr, cnt * 2 + 1);
95+
while (k < n && arr[i] == arr[j] && arr[j] == arr[k]) {
96+
++i;
97+
++j;
98+
++k;
99+
}
100+
if (k == n) {
101+
return new int[]{i - 1, j};
102+
}
103+
return new int[]{-1, -1};
104+
}
105+
106+
private int find(int[] arr, int cnt) {
107+
int s = 0;
108+
for (int i = 0; i < arr.length; ++i) {
109+
s += arr[i];
110+
if (s == cnt) {
111+
return i;
112+
}
113+
}
114+
return -1;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<int> threeEqualParts(vector<int>& arr) {
125+
int n = arr.size();
126+
int cnt1 = accumulate(arr.begin(), arr.end(), 0);
127+
int cnt = cnt1 / 3;
128+
int mod = cnt1 % 3;
129+
if (mod) return {-1, -1};
130+
if (cnt == 0) return {0, n - 1};
131+
int i = find(arr, 1);
132+
int j = find(arr, cnt + 1);
133+
int k = find(arr, cnt * 2 + 1);
134+
while (k < n && arr[i] == arr[j] && arr[j] == arr[k])
135+
{
136+
++i;
137+
++j;
138+
++k;
139+
}
140+
if (k == n) return {i - 1, j};
141+
return {-1, -1};
142+
}
143+
144+
int find(vector<int>& arr, int cnt) {
145+
int s = 0;
146+
for (int i = 0; i < arr.size(); ++i)
147+
{
148+
s += arr[i];
149+
if (s == cnt) return i;
150+
}
151+
return -1;
152+
}
153+
};
154+
```
54155

156+
### **Go**
157+
158+
```go
159+
func threeEqualParts(arr []int) []int {
160+
n := len(arr)
161+
cnt1 := 0
162+
for _, v := range arr {
163+
cnt1 += v
164+
}
165+
cnt := cnt1 / 3
166+
mod := cnt1 % 3
167+
if mod != 0 {
168+
return []int{-1, -1}
169+
}
170+
if cnt == 0 {
171+
return []int{0, n - 1}
172+
}
173+
find := func(cnt int) int {
174+
s := 0
175+
for i, v := range arr {
176+
s += v
177+
if s == cnt {
178+
return i
179+
}
180+
}
181+
return -1
182+
}
183+
i, j, k := find(1), find(cnt+1), find(cnt*2+1)
184+
for k < n && arr[i] == arr[j] && arr[j] == arr[k] {
185+
i++
186+
j++
187+
k++
188+
}
189+
if k == n {
190+
return []int{i - 1, j}
191+
}
192+
return []int{-1, -1}
193+
}
55194
```
56195

57196
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> threeEqualParts(vector<int>& arr) {
4+
int n = arr.size();
5+
int cnt1 = accumulate(arr.begin(), arr.end(), 0);
6+
int cnt = cnt1 / 3;
7+
int mod = cnt1 % 3;
8+
if (mod) return {-1, -1};
9+
if (cnt == 0) return {0, n - 1};
10+
int i = find(arr, 1);
11+
int j = find(arr, cnt + 1);
12+
int k = find(arr, cnt * 2 + 1);
13+
while (k < n && arr[i] == arr[j] && arr[j] == arr[k])
14+
{
15+
++i;
16+
++j;
17+
++k;
18+
}
19+
if (k == n) return {i - 1, j};
20+
return {-1, -1};
21+
}
22+
23+
int find(vector<int>& arr, int cnt) {
24+
int s = 0;
25+
for (int i = 0; i < arr.size(); ++i)
26+
{
27+
s += arr[i];
28+
if (s == cnt) return i;
29+
}
30+
return -1;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
func threeEqualParts(arr []int) []int {
2+
n := len(arr)
3+
cnt1 := 0
4+
for _, v := range arr {
5+
cnt1 += v
6+
}
7+
cnt := cnt1 / 3
8+
mod := cnt1 % 3
9+
if mod != 0 {
10+
return []int{-1, -1}
11+
}
12+
if cnt == 0 {
13+
return []int{0, n - 1}
14+
}
15+
find := func(cnt int) int {
16+
s := 0
17+
for i, v := range arr {
18+
s += v
19+
if s == cnt {
20+
return i
21+
}
22+
}
23+
return -1
24+
}
25+
i, j, k := find(1), find(cnt+1), find(cnt*2+1)
26+
for k < n && arr[i] == arr[j] && arr[j] == arr[k] {
27+
i++
28+
j++
29+
k++
30+
}
31+
if k == n {
32+
return []int{i - 1, j}
33+
}
34+
return []int{-1, -1}
35+
}

0 commit comments

Comments
 (0)