Skip to content

Commit d146932

Browse files
authored
feat: add solutions to lc problem: No.3065 (doocs#2410)
No.3065.Minimum Operations to Exceed Threshold Value I
1 parent 8a60b11 commit d146932

File tree

7 files changed

+125
-8
lines changed

7 files changed

+125
-8
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+
}

0 commit comments

Comments
 (0)