File tree Expand file tree Collapse file tree 7 files changed +125
-8
lines changed
solution/3000-3099/3065.Minimum Operations to Exceed Threshold Value I Expand file tree Collapse file tree 7 files changed +125
-8
lines changed Original file line number Diff line number Diff line change 56
56
57
57
## 解法
58
58
59
- ### 方法一
59
+ ### 方法一:遍历计数
60
+
61
+ 我们只需要遍历一遍数组,统计小于 $k$ 的元素个数即可。
62
+
63
+ 时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
60
64
61
65
<!-- tabs:start -->
62
66
63
67
``` python
64
-
68
+ class Solution :
69
+ def minOperations (self , nums : List[int ], k : int ) -> int :
70
+ return sum (x < k for x in nums)
65
71
```
66
72
67
73
``` 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
+ }
69
85
```
70
86
71
87
``` 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
+ };
73
100
```
74
101
75
102
```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
+ ```
76
112
113
+ ``` ts
114
+ function minOperations(nums : number [], k : number ): number {
115
+ return nums .filter (x => x < k ).length ;
116
+ }
77
117
```
78
118
79
119
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -52,24 +52,64 @@ It can be shown that 3 is the minimum number of operations needed so that all el
52
52
53
53
## Solutions
54
54
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)$.
56
60
57
61
<!-- tabs:start -->
58
62
59
63
``` python
60
-
64
+ class Solution :
65
+ def minOperations (self , nums : List[int ], k : int ) -> int :
66
+ return sum (x < k for x in nums)
61
67
```
62
68
63
69
``` 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
+ }
65
81
```
66
82
67
83
``` 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
+ };
69
96
```
70
97
71
98
```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
+ ```
72
108
109
+ ``` ts
110
+ function minOperations(nums : number [], k : number ): number {
111
+ return nums .filter (x => x < k ).length ;
112
+ }
73
113
```
74
114
75
115
<!-- tabs: end -->
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minOperations (self , nums : List [int ], k : int ) -> int :
3
+ return sum (x < k for x in nums )
Original file line number Diff line number Diff line change
1
+ function minOperations ( nums : number [ ] , k : number ) : number {
2
+ return nums . filter ( x => x < k ) . length ;
3
+ }
You can’t perform that action at this time.
0 commit comments