[InterviewBit] Partitions

Partitions

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Solution::solve(int A, vector<int> &B) {
long long sum = 0;
for(auto b : B) sum += b;
if(sum % 3) return 0;
long long t = sum / 3;
long long now = 0;
int res = 0, dp = 0;
for(int i = 0; i < A - 1; i++) {
now += B[i];
if(now == t * 2) res += dp;
if(now == t) dp += 1;
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/25/PS/interviewbit/partitions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.