Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.3066
Browse files Browse the repository at this point in the history
No.3066.Minimum Operations to Exceed Threshold Value II
  • Loading branch information
yanglbme committed Jan 15, 2025
1 parent cd0409a commit d7e4b95
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Solution:
ans = 0
while len(nums) > 1 and nums[0] < k:
x, y = heappop(nums), heappop(nums)
heappush(nums, min(x, y) * 2 + max(x, y))
heappush(nums, x * 2 + y)
ans += 1
return ans
```
Expand All @@ -112,7 +112,7 @@ class Solution {
int ans = 0;
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
long x = pq.poll(), y = pq.poll();
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
pq.offer(x * 2 + y);
}
return ans;
}
Expand All @@ -136,7 +136,7 @@ public:
pq.pop();
ll y = pq.top();
pq.pop();
pq.push(min(x, y) * 2 + max(x, y));
pq.push(x * 2 + y);
}
return ans;
}
Expand All @@ -151,7 +151,7 @@ func minOperations(nums []int, k int) (ans int) {
heap.Init(pq)
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
heap.Push(pq, min(x, y)*2+max(x, y))
heap.Push(pq, x*2+y)
}
return
}
Expand Down Expand Up @@ -183,12 +183,39 @@ function minOperations(nums: number[], k: number): number {
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
const x = pq.dequeue().element;
const y = pq.dequeue().element;
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
pq.enqueue(x * 2 + y);
}
return ans;
}
```

#### Rust

```rust
use std::collections::BinaryHeap;

impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
let mut pq = BinaryHeap::new();

for &x in &nums {
pq.push(-(x as i64));
}

let mut ans = 0;

while pq.len() > 1 && -pq.peek().unwrap() < k as i64 {
let x = -pq.pop().unwrap();
let y = -pq.pop().unwrap();
pq.push(-(x * 2 + y));
ans += 1;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Solution:
ans = 0
while len(nums) > 1 and nums[0] < k:
x, y = heappop(nums), heappop(nums)
heappush(nums, min(x, y) * 2 + max(x, y))
heappush(nums, x * 2 + y)
ans += 1
return ans
```
Expand All @@ -110,7 +110,7 @@ class Solution {
int ans = 0;
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
long x = pq.poll(), y = pq.poll();
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
pq.offer(x * 2 + y);
}
return ans;
}
Expand All @@ -134,7 +134,7 @@ public:
pq.pop();
ll y = pq.top();
pq.pop();
pq.push(min(x, y) * 2 + max(x, y));
pq.push(x * 2 + y);
}
return ans;
}
Expand All @@ -149,7 +149,7 @@ func minOperations(nums []int, k int) (ans int) {
heap.Init(pq)
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
heap.Push(pq, min(x, y)*2+max(x, y))
heap.Push(pq, x*2+y)
}
return
}
Expand Down Expand Up @@ -181,12 +181,39 @@ function minOperations(nums: number[], k: number): number {
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
const x = pq.dequeue().element;
const y = pq.dequeue().element;
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
pq.enqueue(x * 2 + y);
}
return ans;
}
```

#### Rust

```rust
use std::collections::BinaryHeap;

impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
let mut pq = BinaryHeap::new();

for &x in &nums {
pq.push(-(x as i64));
}

let mut ans = 0;

while pq.len() > 1 && -pq.peek().unwrap() < k as i64 {
let x = -pq.pop().unwrap();
let y = -pq.pop().unwrap();
pq.push(-(x * 2 + y));
ans += 1;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Solution {
pq.pop();
ll y = pq.top();
pq.pop();
pq.push(min(x, y) * 2 + max(x, y));
pq.push(x * 2 + y);
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ func minOperations(nums []int, k int) (ans int) {
heap.Init(pq)
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
heap.Push(pq, min(x, y)*2+max(x, y))
heap.Push(pq, x*2+y)
}
return
}
Expand All @@ -20,4 +20,4 @@ func (h *hp) Pop() interface{} {
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public int minOperations(int[] nums, int k) {
int ans = 0;
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
long x = pq.poll(), y = pq.poll();
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
pq.offer(x * 2 + y);
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def minOperations(self, nums: List[int], k: int) -> int:
ans = 0
while len(nums) > 1 and nums[0] < k:
x, y = heappop(nums), heappop(nums)
heappush(nums, min(x, y) * 2 + max(x, y))
heappush(nums, x * 2 + y)
ans += 1
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::collections::BinaryHeap;

impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
let mut pq = BinaryHeap::new();

for &x in &nums {
pq.push(-(x as i64));
}

let mut ans = 0;

while pq.len() > 1 && -pq.peek().unwrap() < k as i64 {
let x = -pq.pop().unwrap();
let y = -pq.pop().unwrap();
pq.push(-(x * 2 + y));
ans += 1;
}

ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function minOperations(nums: number[], k: number): number {
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
const x = pq.dequeue().element;
const y = pq.dequeue().element;
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
pq.enqueue(x * 2 + y);
}
return ans;
}

0 comments on commit d7e4b95

Please sign in to comment.