Skip to content

Commit 2e385b5

Browse files
authored
feat: add solutions to lc problem: No.3066 (doocs#2411)
No.3066.Minimum Operations to Exceed Threshold Value II
1 parent d146932 commit 2e385b5

File tree

7 files changed

+252
-8
lines changed

7 files changed

+252
-8
lines changed

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

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,107 @@
6060

6161
## 解法
6262

63-
### 方法一
63+
### 方法一:优先队列(小根堆)
64+
65+
我们可以使用优先队列(小根堆)来模拟这个过程。
66+
67+
具体地,我们先将数组中的元素加入优先队列 $pq$ 中。然后我们不断地从优先队列中取出两个最小的元素 $x$ 和 $y$,将 $\min(x, y) \times 2 + \max(x, y)$ 放回优先队列中。每次操作后,我们将操作次数加一。当队列中的元素个数小于 $2$ 或者队列中的最小元素大于等于 $k$ 时,我们停止操作。
68+
69+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
6470

6571
<!-- tabs:start -->
6672

6773
```python
68-
74+
class Solution:
75+
def minOperations(self, nums: List[int], k: int) -> int:
76+
heapify(nums)
77+
ans = 0
78+
while len(nums) > 1 and nums[0] < k:
79+
x, y = heappop(nums), heappop(nums)
80+
heappush(nums, min(x, y) * 2 + max(x, y))
81+
ans += 1
82+
return ans
6983
```
7084

7185
```java
72-
86+
class Solution {
87+
public int minOperations(int[] nums, int k) {
88+
PriorityQueue<Long> pq = new PriorityQueue<>();
89+
for (int x : nums) {
90+
pq.offer((long) x);
91+
}
92+
int ans = 0;
93+
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
94+
long x = pq.poll(), y = pq.poll();
95+
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
96+
}
97+
return ans;
98+
}
99+
}
73100
```
74101

75102
```cpp
76-
103+
class Solution {
104+
public:
105+
int minOperations(vector<int>& nums, int k) {
106+
using ll = long long;
107+
priority_queue<ll, vector<ll>, greater<ll>> pq;
108+
for (int x : nums) {
109+
pq.push(x);
110+
}
111+
int ans = 0;
112+
for (; pq.size() > 1 && pq.top() < k; ++ans) {
113+
ll x = pq.top();
114+
pq.pop();
115+
ll y = pq.top();
116+
pq.pop();
117+
pq.push(min(x, y) * 2 + max(x, y));
118+
}
119+
return ans;
120+
}
121+
};
77122
```
78123
79124
```go
125+
func minOperations(nums []int, k int) (ans int) {
126+
pq := &hp{nums}
127+
heap.Init(pq)
128+
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
129+
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
130+
heap.Push(pq, min(x, y)*2+max(x, y))
131+
}
132+
return
133+
}
134+
135+
type hp struct{ sort.IntSlice }
136+
137+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
138+
func (h *hp) Pop() interface{} {
139+
old := h.IntSlice
140+
n := len(old)
141+
x := old[n-1]
142+
h.IntSlice = old[0 : n-1]
143+
return x
144+
}
145+
func (h *hp) Push(x interface{}) {
146+
h.IntSlice = append(h.IntSlice, x.(int))
147+
}
148+
```
80149

150+
```ts
151+
function minOperations(nums: number[], k: number): number {
152+
const pq = new MinPriorityQueue();
153+
for (const x of nums) {
154+
pq.enqueue(x);
155+
}
156+
let ans = 0;
157+
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
158+
const x = pq.dequeue().element;
159+
const y = pq.dequeue().element;
160+
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
161+
}
162+
return ans;
163+
}
81164
```
82165

83166
<!-- tabs:end -->

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

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,107 @@ It can be shown that 4 is the minimum number of operations needed so that all el
5656

5757
## Solutions
5858

59-
### Solution 1
59+
### Solution 1: Priority Queue (Min Heap)
60+
61+
We can use a priority queue (min heap) to simulate this process.
62+
63+
Specifically, we first add the elements in the array to the priority queue `pq`. Then we continuously take out the two smallest elements `x` and `y` from the priority queue, and put `min(x, y) * 2 + max(x, y)` back into the priority queue. After each operation, we increase the operation count by one. We stop the operation when the number of elements in the queue is less than 2 or the smallest element in the queue is greater than or equal to `k`.
64+
65+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.
6066

6167
<!-- tabs:start -->
6268

6369
```python
64-
70+
class Solution:
71+
def minOperations(self, nums: List[int], k: int) -> int:
72+
heapify(nums)
73+
ans = 0
74+
while len(nums) > 1 and nums[0] < k:
75+
x, y = heappop(nums), heappop(nums)
76+
heappush(nums, min(x, y) * 2 + max(x, y))
77+
ans += 1
78+
return ans
6579
```
6680

6781
```java
68-
82+
class Solution {
83+
public int minOperations(int[] nums, int k) {
84+
PriorityQueue<Long> pq = new PriorityQueue<>();
85+
for (int x : nums) {
86+
pq.offer((long) x);
87+
}
88+
int ans = 0;
89+
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
90+
long x = pq.poll(), y = pq.poll();
91+
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
92+
}
93+
return ans;
94+
}
95+
}
6996
```
7097

7198
```cpp
72-
99+
class Solution {
100+
public:
101+
int minOperations(vector<int>& nums, int k) {
102+
using ll = long long;
103+
priority_queue<ll, vector<ll>, greater<ll>> pq;
104+
for (int x : nums) {
105+
pq.push(x);
106+
}
107+
int ans = 0;
108+
for (; pq.size() > 1 && pq.top() < k; ++ans) {
109+
ll x = pq.top();
110+
pq.pop();
111+
ll y = pq.top();
112+
pq.pop();
113+
pq.push(min(x, y) * 2 + max(x, y));
114+
}
115+
return ans;
116+
}
117+
};
73118
```
74119
75120
```go
121+
func minOperations(nums []int, k int) (ans int) {
122+
pq := &hp{nums}
123+
heap.Init(pq)
124+
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
125+
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
126+
heap.Push(pq, min(x, y)*2+max(x, y))
127+
}
128+
return
129+
}
130+
131+
type hp struct{ sort.IntSlice }
132+
133+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
134+
func (h *hp) Pop() interface{} {
135+
old := h.IntSlice
136+
n := len(old)
137+
x := old[n-1]
138+
h.IntSlice = old[0 : n-1]
139+
return x
140+
}
141+
func (h *hp) Push(x interface{}) {
142+
h.IntSlice = append(h.IntSlice, x.(int))
143+
}
144+
```
76145

146+
```ts
147+
function minOperations(nums: number[], k: number): number {
148+
const pq = new MinPriorityQueue();
149+
for (const x of nums) {
150+
pq.enqueue(x);
151+
}
152+
let ans = 0;
153+
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
154+
const x = pq.dequeue().element;
155+
const y = pq.dequeue().element;
156+
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
157+
}
158+
return ans;
159+
}
77160
```
78161

79162
<!-- tabs:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int k) {
4+
using ll = long long;
5+
priority_queue<ll, vector<ll>, greater<ll>> pq;
6+
for (int x : nums) {
7+
pq.push(x);
8+
}
9+
int ans = 0;
10+
for (; pq.size() > 1 && pq.top() < k; ++ans) {
11+
ll x = pq.top();
12+
pq.pop();
13+
ll y = pq.top();
14+
pq.pop();
15+
pq.push(min(x, y) * 2 + max(x, y));
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func minOperations(nums []int, k int) (ans int) {
2+
pq := &hp{nums}
3+
heap.Init(pq)
4+
for ; pq.Len() > 1 && pq.IntSlice[0] < k; ans++ {
5+
x, y := heap.Pop(pq).(int), heap.Pop(pq).(int)
6+
heap.Push(pq, min(x, y)*2+max(x, y))
7+
}
8+
return
9+
}
10+
11+
type hp struct{ sort.IntSlice }
12+
13+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
14+
func (h *hp) Pop() interface{} {
15+
old := h.IntSlice
16+
n := len(old)
17+
x := old[n-1]
18+
h.IntSlice = old[0 : n-1]
19+
return x
20+
}
21+
func (h *hp) Push(x interface{}) {
22+
h.IntSlice = append(h.IntSlice, x.(int))
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int minOperations(int[] nums, int k) {
3+
PriorityQueue<Long> pq = new PriorityQueue<>();
4+
for (int x : nums) {
5+
pq.offer((long) x);
6+
}
7+
int ans = 0;
8+
for (; pq.size() > 1 && pq.peek() < k; ++ans) {
9+
long x = pq.poll(), y = pq.poll();
10+
pq.offer(Math.min(x, y) * 2 + Math.max(x, y));
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int], k: int) -> int:
3+
heapify(nums)
4+
ans = 0
5+
while len(nums) > 1 and nums[0] < k:
6+
x, y = heappop(nums), heappop(nums)
7+
heappush(nums, min(x, y) * 2 + max(x, y))
8+
ans += 1
9+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minOperations(nums: number[], k: number): number {
2+
const pq = new MinPriorityQueue();
3+
for (const x of nums) {
4+
pq.enqueue(x);
5+
}
6+
let ans = 0;
7+
for (; pq.size() > 1 && pq.front().element < k; ++ans) {
8+
const x = pq.dequeue().element;
9+
const y = pq.dequeue().element;
10+
pq.enqueue(Math.min(x, y) * 2 + Math.max(x, y));
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)