File tree 1 file changed +39
-0
lines changed
scripts/algorithms/C/Construct Target Array With Multiple Sums
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 4 ms (Top 66.6%) | Memory: 2.94 MB (Top 66.6%)
2
+
3
+ type Num = u64 ;
4
+
5
+ use std:: collections:: BinaryHeap ;
6
+
7
+ impl Solution {
8
+ fn preproc ( vec : Vec < i32 > ) -> Vec < Num > {
9
+ vec. into_iter ( )
10
+ . map ( |e| e as Num )
11
+ . collect :: < Vec < _ > > ( )
12
+ }
13
+
14
+ pub fn is_possible ( vec : Vec < i32 > ) -> bool {
15
+ let vec = Self :: preproc ( vec) ;
16
+
17
+ let mut heap_sum = vec. iter ( ) . sum :: < Num > ( ) ;
18
+ let mut heap = BinaryHeap :: from ( vec) ;
19
+ while let Some ( e) = heap. pop ( ) {
20
+ heap_sum -= e;
21
+ if e <= heap_sum || heap_sum < 1 {
22
+ heap. push ( e) ;
23
+ break ;
24
+ }
25
+
26
+ let mut e_next = e % heap_sum;
27
+ if e_next < 1 {
28
+ e_next += heap_sum;
29
+ }
30
+
31
+ heap_sum += e_next;
32
+ heap. push ( e_next) ;
33
+ }
34
+
35
+ heap. into_iter ( )
36
+ . filter ( |& e| e != 1 )
37
+ . count ( ) < 1
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments