-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0331-2.cpp
More file actions
42 lines (40 loc) · 939 Bytes
/
0331-2.cpp
File metadata and controls
42 lines (40 loc) · 939 Bytes
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
#include <string>
#include <vector>
#include <queue>
using namespace std;
queue<long long> q1;
queue<long long> q2;
int solution(vector<int> queue1, vector<int> queue2) {
long long sum1 = 0, sum2 = 0;
for(int i = 0; i < queue1.size(); i++)
{
q1.push(queue1[i]);
sum1 += (long long)queue1[i];
q2.push(queue2[i]);
sum2 += (long long)queue2[i];
}
if((sum1 + sum2) % 2 == 1) return -1;
int cnt = 0;
int limit = 4 * queue1.size();
while(cnt < limit)
{
if(sum1 == sum2) return cnt;
cnt++;
if(sum1 > sum2)
{
q2.push(q1.front());
sum2 += q1.front();
sum1 -= q1.front();
q1.pop();
continue;
}
if(sum1 < sum2)
{
q1.push(q2.front());
sum1 += q2.front();
sum2 -= q2.front();
q2.pop();
}
}
return -1;
}