반응형
https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/
int 벡터를 공백이 아니고 합이 같은 세 개의 부분 배열로 나눌 수 있는지 묻는 문제.
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& A) {
int sum = 0;
for(auto i:A)
sum+=i;
if(sum %3 != 0)
return false;
int groupNum = 0;
int cnt = 0;
for(int i = 0; i<A.size(); i++){
cnt+=A[i];
if(cnt== (sum/3)){
cnt =0;
groupNum++;
}
}
if(groupNum >= 3) //★
return true;
else
return false;
}
};
{10,-10, 10,-10, 10,-10., 10,-10}의 경우
0이 4번 나오게 4개로도 나눌 수 있지만 3개(0, 0, (0+0))로도 나눌 수 있기 때문에
나눌 수 있는 부분이 3개이상이면 true
반응형
'IT > 알고리즘' 카테고리의 다른 글
31] Leetcode 1512. Number of Good Pairs (0) | 2020.07.12 |
---|---|
30★★] Leetcode 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2020.07.10 |
28] Leetcode 1021. Remove Outermost Parentheses (0) | 2020.07.06 |
27] Leetcode 1266. Minimum Time Visiting All Points (0) | 2020.07.06 |
26★] Leetcode 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2020.07.05 |
댓글