File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
December 2024 Leetcode Solution Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ long long pickGifts (int * gifts , int giftsSize , int k ) {
2
+ for (int i = (giftsSize - 2 ) / 2 ; i >= 0 ; i -- ) {
3
+ int current = i ;
4
+ while (current * 2 + 1 < giftsSize ) {
5
+ int child = current * 2 + 1 ;
6
+ if (child + 1 < giftsSize && gifts [child ] < gifts [child + 1 ]) {
7
+ child ++ ;
8
+ }
9
+ if (gifts [current ] >= gifts [child ]) {
10
+ break ;
11
+ }
12
+ int temp = gifts [current ];
13
+ gifts [current ] = gifts [child ];
14
+ gifts [child ] = temp ;
15
+ current = child ;
16
+ }
17
+ }
18
+
19
+ for (int i = 0 ; i < k ; i ++ ) {
20
+ gifts [0 ] = (int )sqrt (gifts [0 ]);
21
+ int current = 0 ;
22
+ while (current * 2 + 1 < giftsSize ) {
23
+ int child = current * 2 + 1 ;
24
+ if (child + 1 < giftsSize && gifts [child ] < gifts [child + 1 ]) {
25
+ child ++ ;
26
+ }
27
+ if (gifts [current ] >= gifts [child ]) {
28
+ break ;
29
+ }
30
+ int temp = gifts [current ];
31
+ gifts [current ] = gifts [child ];
32
+ gifts [child ] = temp ;
33
+ current = child ;
34
+ }
35
+ }
36
+
37
+ long long sum = 0 ;
38
+ for (int i = 0 ; i < giftsSize ; i ++ ) {
39
+ sum += gifts [i ];
40
+ }
41
+ return sum ;
42
+ }
You can’t perform that action at this time.
0 commit comments