Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9373d73

Browse files
feat: add solutions to lc problem: No.3066 (doocs#3953)
No.3066.Minimum Operations to Exceed Threshold Value II
1 parent cd0409a commit 9373d73

File tree

8 files changed

+94
-18
lines changed

8 files changed

+94
-18
lines changed

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README.md‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Solution:
9595
ans = 0
9696
while len(nums) > 1 and nums[0] < k:
9797
x, y = heappop(nums), heappop(nums)
98-
heappush(nums, min(x, y) * 2 + max(x, y))
98+
heappush(nums, x * 2 + y)
9999
ans += 1
100100
return ans
101101
```
@@ -112,7 +112,7 @@ class Solution {
112112
int ans = 0;
113113
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
114114
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);
116116
}
117117
return ans;
118118
}
@@ -136,7 +136,7 @@ public:
136136
pq.pop();
137137
ll y = pq.top();
138138
pq.pop();
139-
pq.push(min(x, y) * 2 + max(x, y));
139+
pq.push(x * 2 + y);
140140
}
141141
return ans;
142142
}
@@ -151,7 +151,7 @@ func minOperations(nums []int, k int) (ans int) {
151151
heap.Init(pq)
152152
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
153153
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)
155155
}
156156
return
157157
}
@@ -183,12 +183,39 @@ function minOperations(nums: number[], k: number): number {
183183
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
184184
const x = pq.dequeue().element;
185185
const y = pq.dequeue().element;
186-
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
186+
pq.enqueue(x* 2 + y);
187187
}
188188
return ans;
189189
}
190190
```
191191

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+
192219
<!-- tabs:end -->
193220

194221
<!-- solution:end -->

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README_EN.md‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Solution:
9393
ans = 0
9494
while len(nums) > 1 and nums[0] < k:
9595
x, y = heappop(nums), heappop(nums)
96-
heappush(nums, min(x, y) * 2 + max(x, y))
96+
heappush(nums, x * 2 + y)
9797
ans += 1
9898
return ans
9999
```
@@ -110,7 +110,7 @@ class Solution {
110110
int ans = 0;
111111
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
112112
long x = pq.poll(), y = pq.poll();
113-
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
113+
pq.offer(x * 2 + y);
114114
}
115115
return ans;
116116
}
@@ -134,7 +134,7 @@ public:
134134
pq.pop();
135135
ll y = pq.top();
136136
pq.pop();
137-
pq.push(min(x, y) * 2 + max(x, y));
137+
pq.push(x * 2 + y);
138138
}
139139
return ans;
140140
}
@@ -149,7 +149,7 @@ func minOperations(nums []int, k int) (ans int) {
149149
heap.Init(pq)
150150
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
151151
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
152-
heap.Push(pq, min(x, y)*2+max(x, y))
152+
heap.Push(pq, x*2+y)
153153
}
154154
return
155155
}
@@ -181,12 +181,39 @@ function minOperations(nums: number[], k: number): number {
181181
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
182182
const x = pq.dequeue().element;
183183
const y = pq.dequeue().element;
184-
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
184+
pq.enqueue(x* 2 + y);
185185
}
186186
return ans;
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
use std::collections::BinaryHeap;
194+
195+
impl Solution {
196+
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
197+
let mut pq = BinaryHeap::new();
198+
199+
for &x in &nums {
200+
pq.push(-(x as i64));
201+
}
202+
203+
let mut ans = 0;
204+
205+
while pq.len() > 1 && -pq.peek().unwrap() < k as i64 {
206+
let x = -pq.pop().unwrap();
207+
let y = -pq.pop().unwrap();
208+
pq.push(-(x * 2 + y));
209+
ans += 1;
210+
}
211+
212+
ans
213+
}
214+
}
215+
```
216+
190217
<!-- tabs:end -->
191218

192219
<!-- solution:end -->

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Solution {
1212
pq.pop();
1313
ll y = pq.top();
1414
pq.pop();
15-
pq.push(min(x, y) * 2 + max(x, y));
15+
pq.push(x * 2 + y);
1616
}
1717
return ans;
1818
}
19-
};
19+
};

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ func minOperations(nums []int, k int) (ans int) {
33
heap.Init(pq)
44
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
55
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
6-
heap.Push(pq, min(x, y)*2+max(x, y))
6+
heap.Push(pq, x*2+y)
77
}
88
return
99
}
@@ -20,4 +20,4 @@ func (h *hp) Pop() interface{} {
2020
}
2121
func (h *hp) Push(x interface{}) {
2222
h.IntSlice = append(h.IntSlice, x.(int))
23-
}
23+
}

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public int minOperations(int[] nums, int k) {
77
int ans = 0;
88
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
99
long x = pq.poll(), y = pq.poll();
10-
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
10+
pq.offer(x* 2 + y);
1111
}
1212
return ans;
1313
}
14-
}
14+
}

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def minOperations(self, nums: List[int], k: int) -> int:
44
ans = 0
55
while len(nums) > 1 and nums[0] < k:
66
x, y = heappop(nums), heappop(nums)
7-
heappush(nums, min(x, y) * 2 + max(x, y))
7+
heappush(nums, x* 2 + y)
88
ans += 1
99
return ans
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::BinaryHeap;
2+
3+
impl Solution {
4+
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
5+
let mut pq = BinaryHeap::new();
6+
7+
for &x in &nums {
8+
pq.push(-(x as i64));
9+
}
10+
11+
let mut ans = 0;
12+
13+
while pq.len() > 1 && -pq.peek().unwrap() < k as i64 {
14+
let x = -pq.pop().unwrap();
15+
let y = -pq.pop().unwrap();
16+
pq.push(-(x * 2 + y));
17+
ans += 1;
18+
}
19+
20+
ans
21+
}
22+
}

‎solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function minOperations(nums: number[], k: number): number {
77
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
88
const x = pq.dequeue().element;
99
const y = pq.dequeue().element;
10-
pq.enqueue(Math.min(x,y)* 2 + Math.max(x,y));
10+
pq.enqueue(x* 2 + y);
1111
}
1212
return ans;
1313
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /