36
36
37
37
<!-- 这里可写通用的实现逻辑 -->
38
38
39
- 统计各个问题类型出现的次数,按照次数降序排列。
39
+ ** 方法一:计数 + 排序 **
40
40
41
- 然后依次选择问题类型,直至满足条件。
41
+ 我们可以用哈希表或数组 ` cnt ` 统计每种知识点类型的题目数量,然后对 ` cnt ` 进行排序,从大到小遍历 ` cnt ` ,直到遍历的题目数量之和大于等于 ` n ` 即可,此时遍历的次数即为所求。
42
+
43
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为 ` questions ` 的长度。
42
44
43
45
<!-- tabs:start -->
44
46
49
51
``` python
50
52
class Solution :
51
53
def halfQuestions (self , questions : List[int ]) -> int :
52
- counter = Counter(questions)
53
- n = len (questions) >> 1
54
- ans = 0
55
- for i, v in counter.most_common():
54
+ cnt = Counter(questions)
55
+ ans, n = 0 , len (questions) >> 1
56
+ for _, v in cnt.most_common():
56
57
ans += 1
57
- if v >= n:
58
- return ans
59
58
n -= v
59
+ if n <= 0 :
60
+ break
60
61
return ans
61
62
```
62
63
@@ -67,16 +68,16 @@ class Solution:
67
68
``` java
68
69
class Solution {
69
70
public int halfQuestions (int [] questions ) {
70
- int [] counter = new int [1010 ];
71
- for (int q : questions) {
72
- ++ counter[q ];
71
+ int [] cnt = new int [1010 ];
72
+ for (int x : questions) {
73
+ ++ cnt[x ];
73
74
}
74
- Arrays . sort(counter );
75
+ Arrays . sort(cnt );
75
76
int ans = 0 ;
76
77
int n = questions. length >> 1 ;
77
- for (int i = counter . length - 1 ; n > 0 ; -- i) {
78
+ for (int i = cnt . length - 1 ; n > 0 ; -- i) {
78
79
++ ans;
79
- n -= counter [i];
80
+ n -= cnt [i];
80
81
}
81
82
return ans;
82
83
}
@@ -89,14 +90,15 @@ class Solution {
89
90
class Solution {
90
91
public:
91
92
int halfQuestions(vector<int >& questions) {
92
- vector<int > counter(1010);
93
- for (int q : questions) ++counter[ q] ;
94
- int n = questions.size() >> 1;
95
- sort(counter.begin(), counter.end());
96
- int ans = 0;
97
- for (int i = counter.size() - 1; n > 0; --i) {
93
+ int cnt[ 1001] {};
94
+ for (int& x : questions) {
95
+ ++cnt[ x] ;
96
+ }
97
+ sort(cnt, cnt + 1001);
98
+ int ans = 0, n = questions.size() / 2;
99
+ for (int i = 1000; n > 0; --i) {
98
100
++ans;
99
- n -= counter [ i] ;
101
+ n -= cnt [ i] ;
100
102
}
101
103
return ans;
102
104
}
@@ -106,19 +108,18 @@ public:
106
108
### **Go**
107
109
108
110
```go
109
- func halfQuestions(questions []int) int {
110
- counter := make([]int, 1010)
111
- for _, q := range questions {
112
- counter[q ]++
111
+ func halfQuestions(questions []int) (ans int) {
112
+ cnt := make([]int, 1010)
113
+ for _, x := range questions {
114
+ cnt[x ]++
113
115
}
114
116
n := len(questions) >> 1
115
- sort.Ints(counter)
116
- ans := 0
117
- for i := len(counter) - 1; n > 0; i-- {
117
+ sort.Ints(cnt)
118
+ for i := len(cnt) - 1; n > 0; i-- {
118
119
ans++
119
- n -= counter [i]
120
+ n -= cnt [i]
120
121
}
121
- return ans
122
+ return
122
123
}
123
124
```
124
125
@@ -130,21 +131,40 @@ func halfQuestions(questions []int) int {
130
131
* @return {number}
131
132
*/
132
133
var halfQuestions = function (questions ) {
133
- let counter = new Array (1010 ).fill (0 );
134
- for (const q of questions) {
135
- ++ counter[q ];
134
+ const cnt = new Array (1010 ).fill (0 );
135
+ for (const x of questions) {
136
+ ++ cnt[x ];
136
137
}
137
- counter .sort ((a , b ) => b - a);
138
+ cnt .sort ((a , b ) => b - a);
138
139
let ans = 0 ;
139
140
let n = questions .length >> 1 ;
140
141
for (let i = 0 ; n > 0 ; ++ i) {
141
142
++ ans;
142
- n -= counter [i];
143
+ n -= cnt [i];
143
144
}
144
145
return ans;
145
146
};
146
147
```
147
148
149
+ ### ** TypeScript**
150
+
151
+ ``` ts
152
+ function halfQuestions(questions : number []): number {
153
+ const cnt = new Array (1010 ).fill (0 );
154
+ for (const x of questions ) {
155
+ ++ cnt [x ];
156
+ }
157
+ cnt .sort ((a , b ) => b - a );
158
+ let ans = 0 ;
159
+ let n = questions .length >> 1 ;
160
+ for (let i = 0 ; n > 0 ; ++ i ) {
161
+ ++ ans ;
162
+ n -= cnt [i ];
163
+ }
164
+ return ans ;
165
+ }
166
+ ```
167
+
148
168
### ** ...**
149
169
150
170
```
0 commit comments