Skip to content

Commit df93126

Browse files
authored
Update 1354.Construct-Target-Array-With-Multiple-Sums.cpp
1 parent ca1d77a commit df93126

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

Greedy/1354.Construct-Target-Array-With-Multiple-Sums/1354.Construct-Target-Array-With-Multiple-Sums.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ class Solution {
22
public:
33
bool isPossible(vector<int>& target)
44
{
5-
long long sum = 0;
65
priority_queue<int>pq;
7-
for (int i=0; i<target.size(); i++)
8-
{
9-
pq.push(target[i]);
10-
sum+=target[i];
11-
}
6+
for (auto x: target) pq.push(x);
7+
long long sum = 0;
8+
for (auto x: target)
9+
sum += x;
1210

1311
while (pq.top()!=1)
1412
{
1513
long long a = pq.top();
16-
long long others = sum - a;
1714
pq.pop();
18-
19-
if (others ==0) return false;
20-
if (a - others <= 0) return false;
15+
long long others = sum - a;
16+
17+
if (others == 0) return false;
18+
if (a<=others) return false;
2119
long long b = a % others;
20+
if (b==0) b = others;
2221

23-
sum = others + b;
24-
pq.push(b);
25-
}
26-
return true;
22+
sum = others+b;
23+
pq.push(b);
24+
}
25+
26+
return true;
2727
}
2828
};

0 commit comments

Comments
 (0)