Skip to content

Commit fd78ade

Browse files
authored
Merge branch 'doocs:main' into main
2 parents ea6c627 + 7cb44df commit fd78ade

File tree

21 files changed

+864
-24
lines changed

21 files changed

+864
-24
lines changed

solution/3000-3099/3065.Minimum Operations to Exceed Threshold Value I/README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,64 @@
5656

5757
## 解法
5858

59-
### 方法一
59+
### 方法一:遍历计数
60+
61+
我们只需要遍历一遍数组,统计小于 $k$ 的元素个数即可。
62+
63+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
6064

6165
<!-- tabs:start -->
6266

6367
```python
64-
68+
class Solution:
69+
def minOperations(self, nums: List[int], k: int) -> int:
70+
return sum(x < k for x in nums)
6571
```
6672

6773
```java
68-
74+
class Solution {
75+
public int minOperations(int[] nums, int k) {
76+
int ans = 0;
77+
for (int x : nums) {
78+
if (x < k) {
79+
++ans;
80+
}
81+
}
82+
return ans;
83+
}
84+
}
6985
```
7086

7187
```cpp
72-
88+
class Solution {
89+
public:
90+
int minOperations(vector<int>& nums, int k) {
91+
int ans = 0;
92+
for (int x : nums) {
93+
if (x < k) {
94+
++ans;
95+
}
96+
}
97+
return ans;
98+
}
99+
};
73100
```
74101
75102
```go
103+
func minOperations(nums []int, k int) (ans int) {
104+
for _, x := range nums {
105+
if x < k {
106+
ans++
107+
}
108+
}
109+
return
110+
}
111+
```
76112

113+
```ts
114+
function minOperations(nums: number[], k: number): number {
115+
return nums.filter(x => x < k).length;
116+
}
77117
```
78118

79119
<!-- tabs:end -->

solution/3000-3099/3065.Minimum Operations to Exceed Threshold Value I/README_EN.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,64 @@ It can be shown that 3 is the minimum number of operations needed so that all el
5252

5353
## Solutions
5454

55-
### Solution 1
55+
### Solution 1: Traversal and Counting
56+
57+
We only need to traverse the array once, counting the number of elements less than $k$.
58+
59+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
5660

5761
<!-- tabs:start -->
5862

5963
```python
60-
64+
class Solution:
65+
def minOperations(self, nums: List[int], k: int) -> int:
66+
return sum(x < k for x in nums)
6167
```
6268

6369
```java
64-
70+
class Solution {
71+
public int minOperations(int[] nums, int k) {
72+
int ans = 0;
73+
for (int x : nums) {
74+
if (x < k) {
75+
++ans;
76+
}
77+
}
78+
return ans;
79+
}
80+
}
6581
```
6682

6783
```cpp
68-
84+
class Solution {
85+
public:
86+
int minOperations(vector<int>& nums, int k) {
87+
int ans = 0;
88+
for (int x : nums) {
89+
if (x < k) {
90+
++ans;
91+
}
92+
}
93+
return ans;
94+
}
95+
};
6996
```
7097
7198
```go
99+
func minOperations(nums []int, k int) (ans int) {
100+
for _, x := range nums {
101+
if x < k {
102+
ans++
103+
}
104+
}
105+
return
106+
}
107+
```
72108

109+
```ts
110+
function minOperations(nums: number[], k: number): number {
111+
return nums.filter(x => x < k).length;
112+
}
73113
```
74114

75115
<!-- tabs:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int k) {
4+
int ans = 0;
5+
for (int x : nums) {
6+
if (x < k) {
7+
++ans;
8+
}
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func minOperations(nums []int, k int) (ans int) {
2+
for _, x := range nums {
3+
if x < k {
4+
ans++
5+
}
6+
}
7+
return
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int minOperations(int[] nums, int k) {
3+
int ans = 0;
4+
for (int x : nums) {
5+
if (x < k) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int], k: int) -> int:
3+
return sum(x < k for x in nums)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function minOperations(nums: number[], k: number): number {
2+
return nums.filter(x => x < k).length;
3+
}

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 -->

0 commit comments

Comments
 (0)