Skip to content

feat: add solutions to lc problem: No.3066 #3953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading