@@ -57,13 +57,19 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
57
57
58
58
<!-- 这里可写通用的实现逻辑 -->
59
59
60
- ** 方法一:排序 + 二分查找 **
60
+ ** 方法一:暴力枚举 **
61
61
62
- 对 nums 进行排序 。
62
+ 在 $ [ 1..n ] $ 范围内枚举 $x$,然后统计数组中大于等于 $x$ 的元素个数,记为 $cnt$。若存在 $cnt$ 与 $x$ 相等,直接返回 $x$ 。
63
63
64
- 接下来在 ` [0, n] ` 范围内遍历 x,判断 nums 中是否存在大于等于 x 的个数 cnt,使得 ` cnt == x ` 。若存在,直接范围 x 。
64
+ 时间复杂度 $O(n^2)$ 。
65
65
66
- 否则遍历结束返回 -1。
66
+ ** 方法二:排序 + 二分查找**
67
+
68
+ 我们也可以先对 ` nums ` 进行排序。
69
+
70
+ 接下来同样枚举 $x$,利用二分查找,找到 ` nums ` 中第一个大于等于 $x$ 的元素,快速统计出 ` nums ` 中大于等于 $x$ 的元素个数。
71
+
72
+ 时间复杂度 $O(n\log n)$。
67
73
68
74
<!-- tabs:start -->
69
75
@@ -74,11 +80,20 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
74
80
``` python
75
81
class Solution :
76
82
def specialArray (self , nums : List[int ]) -> int :
77
- n = len (nums)
83
+ for x in range (1 , len (nums) + 1 ):
84
+ cnt = sum (v >= x for v in nums)
85
+ if cnt == x:
86
+ return x
87
+ return - 1
88
+ ```
89
+
90
+ ``` python
91
+ class Solution :
92
+ def specialArray (self , nums : List[int ]) -> int :
78
93
nums.sort()
79
- for x in range (n + 1 ):
80
- idx = bisect_left(nums, x)
81
- cnt = n - 1 - idx + 1
94
+ n = len (nums)
95
+ for x in range ( 1 , n + 1 ):
96
+ cnt = n - bisect_left(nums, x)
82
97
if cnt == x:
83
98
return x
84
99
return - 1
@@ -88,12 +103,31 @@ class Solution:
88
103
89
104
<!-- 这里可写当前语言的特殊实现逻辑 -->
90
105
106
+ ``` java
107
+ class Solution {
108
+ public int specialArray (int [] nums ) {
109
+ for (int x = 1 ; x <= nums. length; ++ x) {
110
+ int cnt = 0 ;
111
+ for (int v : nums) {
112
+ if (v >= x) {
113
+ ++ cnt;
114
+ }
115
+ }
116
+ if (cnt == x) {
117
+ return x;
118
+ }
119
+ }
120
+ return - 1 ;
121
+ }
122
+ }
123
+ ```
124
+
91
125
``` java
92
126
class Solution {
93
127
public int specialArray (int [] nums ) {
94
128
Arrays . sort(nums);
95
129
int n = nums. length;
96
- for (int x = 0 ; x <= n; ++ x) {
130
+ for (int x = 1 ; x <= n; ++ x) {
97
131
int left = 0 , right = n;
98
132
while (left < right) {
99
133
int mid = (left + right) >> 1 ;
@@ -103,7 +137,7 @@ class Solution {
103
137
left = mid + 1 ;
104
138
}
105
139
}
106
- int cnt = n - 1 - left + 1 ;
140
+ int cnt = n - left;
107
141
if (cnt == x) {
108
142
return x;
109
143
}
@@ -115,15 +149,28 @@ class Solution {
115
149
116
150
### ** C++**
117
151
152
+ ``` cpp
153
+ class Solution {
154
+ public:
155
+ int specialArray(vector<int >& nums) {
156
+ for (int x = 1; x <= nums.size(); ++x) {
157
+ int cnt = 0;
158
+ for (int v : nums) cnt += v >= x;
159
+ if (cnt == x) return x;
160
+ }
161
+ return -1;
162
+ }
163
+ };
164
+ ```
165
+
118
166
```cpp
119
167
class Solution {
120
168
public:
121
169
int specialArray(vector<int>& nums) {
122
170
int n = nums.size();
123
171
sort(nums.begin(), nums.end());
124
- for (int x = 0; x <= n; ++x) {
125
- int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
126
- int cnt = n - 1 - idx + 1;
172
+ for (int x = 1; x <= n; ++x) {
173
+ int cnt = n - (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
127
174
if (cnt == x) return x;
128
175
}
129
176
return -1;
@@ -135,9 +182,26 @@ public:
135
182
136
183
``` go
137
184
func specialArray (nums []int ) int {
138
- n := len(nums)
185
+ for x := 1 ; x <= len (nums); x++ {
186
+ cnt := 0
187
+ for _ , v := range nums {
188
+ if v >= x {
189
+ cnt++
190
+ }
191
+ }
192
+ if cnt == x {
193
+ return x
194
+ }
195
+ }
196
+ return -1
197
+ }
198
+ ```
199
+
200
+ ``` go
201
+ func specialArray (nums []int ) int {
139
202
sort.Ints (nums)
140
- for x := 0; x <= n; x++ {
203
+ n := len (nums)
204
+ for x := 1 ; x <= n; x++ {
141
205
left , right := 0 , n
142
206
for left < right {
143
207
mid := (left + right) >> 1
@@ -147,7 +211,7 @@ func specialArray(nums []int) int {
147
211
left = mid + 1
148
212
}
149
213
}
150
- cnt := n - 1 - left + 1
214
+ cnt := n - left
151
215
if cnt == x {
152
216
return x
153
217
}
0 commit comments