File tree Expand file tree Collapse file tree 1 file changed +14
-14
lines changed
Greedy/1354.Construct-Target-Array-With-Multiple-Sums Expand file tree Collapse file tree 1 file changed +14
-14
lines changed Original file line number Diff line number Diff line change @@ -2,27 +2,27 @@ class Solution {
2
2
public:
3
3
bool isPossible (vector<int >& target)
4
4
{
5
- long long sum = 0 ;
6
5
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;
12
10
13
11
while (pq.top ()!=1 )
14
12
{
15
13
long long a = pq.top ();
16
- long long others = sum - a;
17
14
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 ;
21
19
long long b = a % others;
20
+ if (b==0 ) b = others;
22
21
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 ;
27
27
}
28
28
};
You can’t perform that action at this time.
0 commit comments