-
Notifications
You must be signed in to change notification settings - Fork 0
/
1013. Partition Array Into Three Parts With Equal Sum.c
114 lines (100 loc) · 2.25 KB
/
1013. Partition Array Into Three Parts With Equal Sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** C */
bool canThreePartsEqualSum(int* A, int ASize) {
int total=0,i,partition=0,sum=0;
for (i=0;i<ASize;i++) {
total+=*(A+i);
}
if (total%3)
return 0;
total=total/3;
for (i=0;i<ASize;i++) {
sum+=*(A+i);
if (sum==total) {
sum=0;
partition++;
}
}
if (partition==3)
return 1;
else
return 0;
}
bool canThreePartsEqualSum(int* A, int ASize) {
int total=0,sum=0,cnt=0;
for(int i=0;i<ASize;i++)
total+=A[i];
if(total%3!=0) return false;
for(int i=0;i<ASize&&cnt<(total!=0?2:3);i++)
{
sum+=A[i];
if(sum==total/3)
{
cnt++;
sum=0;
}
}
if(cnt==(total!=0?2:3))return true;
else return false;
}
bool canThreePartsEqualSum(int* A, int ASize) {
int i,sum=0,s1,s2,ind1=-1,ind2=-1,s=0;
for(i=0;i<ASize;i++)
sum+=A[i];
if(sum%3!=0) return false;
s1=sum/3;
s2=2*s1;
for(i=0;i<ASize;i++)
{
s+=A[i];
if(s==s1 && ind1==-1 && s!=0)
ind1=i;
else
if(s==s2 && s!=0 && ind1!=-1)
{
ind2=i;
break;
}
}
if (ind1!=-1 && ind2!=-1)
return true;
else
return false;
}
/** Python
class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
sums = sum(A)
if sums % 3 != 0: return False
l, r = 0, sums
for i in xrange(len(A)):
l += A[i]
r -= A[i]
if l * 2 == r:
b = 0
for j in xrange(i+1, len(A)):
b += A[j]
if b == l: return True
return False
class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
total = sum(A)
if total % 3 != 0:
return False
target = total // 3
prefix = 0
goal = [target * 2, target]
for a in A:
prefix += a
if prefix == goal[-1]:
goal.pop()
if not goal:
return True
return False