@@ -95,7 +95,7 @@ class Solution:
95
95
ans = 0
96
96
while len (nums) > 1 and nums[0 ] < k:
97
97
x, y = heappop(nums), heappop(nums)
98
- heappush(nums, min (x, y) * 2 + max (x, y) )
98
+ heappush(nums, x * 2 + y )
99
99
ans += 1
100
100
return ans
101
101
```
@@ -112,7 +112,7 @@ class Solution {
112
112
int ans = 0 ;
113
113
for (; pq. size() > 1 && pq. peek() < k; ++ ans) {
114
114
long x = pq. poll(), y = pq. poll();
115
- pq. offer(Math . min(x, y) * 2 + Math . max(x, y) );
115
+ pq. offer(x * 2 + y );
116
116
}
117
117
return ans;
118
118
}
@@ -136,7 +136,7 @@ public:
136
136
pq.pop();
137
137
ll y = pq.top();
138
138
pq.pop();
139
- pq.push(min(x, y) * 2 + max(x, y) );
139
+ pq.push(x * 2 + y );
140
140
}
141
141
return ans;
142
142
}
@@ -151,7 +151,7 @@ func minOperations(nums []int, k int) (ans int) {
151
151
heap.Init(pq)
152
152
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
153
153
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
154
- heap.Push(pq, min(x, y) *2+max(x, y) )
154
+ heap.Push(pq, x *2+y )
155
155
}
156
156
return
157
157
}
@@ -183,12 +183,39 @@ function minOperations(nums: number[], k: number): number {
183
183
for (; pq .size () > 1 && pq .front ().element < k ; ++ ans ) {
184
184
const x = pq .dequeue ().element ;
185
185
const y = pq .dequeue ().element ;
186
- pq .enqueue (Math . min ( x , y ) * 2 + Math . max ( x , y ) );
186
+ pq .enqueue (x * 2 + y );
187
187
}
188
188
return ans ;
189
189
}
190
190
```
191
191
192
+ #### Rust
193
+
194
+ ``` rust
195
+ use std :: collections :: BinaryHeap ;
196
+
197
+ impl Solution {
198
+ pub fn min_operations (nums : Vec <i32 >, k : i32 ) -> i32 {
199
+ let mut pq = BinaryHeap :: new ();
200
+
201
+ for & x in & nums {
202
+ pq . push (- (x as i64 ));
203
+ }
204
+
205
+ let mut ans = 0 ;
206
+
207
+ while pq . len () > 1 && - pq . peek (). unwrap () < k as i64 {
208
+ let x = - pq . pop (). unwrap ();
209
+ let y = - pq . pop (). unwrap ();
210
+ pq . push (- (x * 2 + y ));
211
+ ans += 1 ;
212
+ }
213
+
214
+ ans
215
+ }
216
+ }
217
+ ```
218
+
192
219
<!-- tabs: end -->
193
220
194
221
<!-- solution: end -->
0 commit comments