From d7e4b95c44ccff45fdb8526424c7c0ab0bf838e1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 15 Jan 2025 12:45:11 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3066 No.3066.Minimum Operations to Exceed Threshold Value II --- .../README.md | 37 ++++++++++++++++--- .../README_EN.md | 37 ++++++++++++++++--- .../Solution.cpp | 4 +- .../Solution.go | 4 +- .../Solution.java | 4 +- .../Solution.py | 2 +- .../Solution.rs | 22 +++++++++++ .../Solution.ts | 2 +- 8 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.rs diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README.md b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README.md index 9e4c56847bf99..7bedee00c45c5 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README.md +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README.md @@ -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 ``` @@ -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; } @@ -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; } @@ -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 } @@ -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, 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 + } +} +``` + diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README_EN.md b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README_EN.md index d1db632834946..67e757a302cab 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README_EN.md +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/README_EN.md @@ -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 ``` @@ -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; } @@ -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; } @@ -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 } @@ -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, 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 + } +} +``` + diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.cpp b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.cpp index b3d9f1d8d5e4e..87b8f913f6c95 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.cpp +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.cpp @@ -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; } -}; \ No newline at end of file +}; diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.go b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.go index 68676f2f46ad5..6705859e0e92f 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.go +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.go @@ -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 } @@ -20,4 +20,4 @@ func (h *hp) Pop() interface{} { } func (h *hp) Push(x interface{}) { h.IntSlice = append(h.IntSlice, x.(int)) -} \ No newline at end of file +} diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.java b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.java index add5acba5e502..80e526b5c7627 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.java +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.py b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.py index ccf597b5169b8..2d730120c034f 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.py +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.py @@ -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 diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.rs b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.rs new file mode 100644 index 0000000000000..572ba21f695d9 --- /dev/null +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.rs @@ -0,0 +1,22 @@ +use std::collections::BinaryHeap; + +impl Solution { + pub fn min_operations(nums: Vec, 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 + } +} diff --git a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.ts b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.ts index 9ac5cafb45cf1..776a2cc4b8c0f 100644 --- a/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.ts +++ b/solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.ts @@ -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; }