File tree 8 files changed +118
-1
lines changed
problemset/special-array-with-x-elements-greater-than-or-equal-x
8 files changed +118
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
<p align =" center " >
4
4
<!-- TOPICS COUNT START -->
5
- <img src =" https://img.shields.io/badge/-进度:491 -green " alt =" 进度:491 " >
5
+ <img src =" https://img.shields.io/badge/-进度:492 -green " alt =" 进度:492 " >
6
6
<!-- TOPICS COUNT END -->
7
7
<a href =" ./assets/docs/TOPICS.md " ><img src =" https://img.shields.io/badge/-题库目录-blue " alt =" 题库目录 " ></a >
8
8
<a href =" ./assets/docs/CATEGORIES.md " ><img src =" https://img.shields.io/badge/-题库分类-red " alt =" 题库分类 " ></a >
Original file line number Diff line number Diff line change 904
904
"path" : " ./problemset/maximum-product-of-two-elements-in-an-array/README.md" ,
905
905
"difficulty" : " 简单"
906
906
},
907
+ {
908
+ "id" : " 1608" ,
909
+ "title" : " 特殊数组的特征值" ,
910
+ "path" : " ./problemset/special-array-with-x-elements-greater-than-or-equal-x/README.md" ,
911
+ "difficulty" : " 简单"
912
+ },
907
913
{
908
914
"id" : " 1984" ,
909
915
"title" : " 学生分数的最小差值" ,
Original file line number Diff line number Diff line change 4529
4529
"url" : " https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/" ,
4530
4530
"path" : " ./problemset/find-servers-that-handled-most-number-of-requests/README.md"
4531
4531
},
4532
+ {
4533
+ "id" : " 1608" ,
4534
+ "title" : {
4535
+ "cn" : " 特殊数组的特征值" ,
4536
+ "en" : " special-array-with-x-elements-greater-than-or-equal-x"
4537
+ },
4538
+ "difficulty" : " 简单" ,
4539
+ "url" : " https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/" ,
4540
+ "path" : " ./problemset/special-array-with-x-elements-greater-than-or-equal-x/README.md"
4541
+ },
4532
4542
{
4533
4543
"id" : " 1656" ,
4534
4544
"title" : {
Original file line number Diff line number Diff line change 171
171
| 1331. [ 数组序号转换] ( ../../problemset/rank-transform-of-an-array/README.md ) | 简单 |
172
172
| 1460. [ 通过翻转子数组使两个数组相等] ( ../../problemset/make-two-arrays-equal-by-reversing-sub-arrays/README.md ) | 简单 |
173
173
| 1464. [ 数组中的两元素的最大乘积] ( ../../problemset/maximum-product-of-two-elements-in-an-array/README.md ) | 简单 |
174
+ | 1608. [ 特殊数组的特征值] ( ../../problemset/special-array-with-x-elements-greater-than-or-equal-x/README.md ) | 简单 |
174
175
| 1984. [ 学生分数的最小差值] ( ../../problemset/minimum-difference-between-highest-and-lowest-of-k-scores/README.md ) | 简单 |
175
176
| 1996. [ 游戏中弱角色的数量] ( ../../problemset/the-number-of-weak-characters-in-the-game/README.md ) | 中等 |
176
177
Original file line number Diff line number Diff line change 906
906
907
907
[ 1606. 找到处理最多请求的服务器] ( ../../problemset/find-servers-that-handled-most-number-of-requests/README.md )
908
908
909
+ [ 1608. 特殊数组的特征值] ( ../../problemset/special-array-with-x-elements-greater-than-or-equal-x/README.md )
910
+
909
911
[ 1656. 涉及有序流] ( ../../problemset/design-an-ordered-stream/README.md )
910
912
911
913
[ 1663. 具有给定数值的最小字符串] ( ../../problemset/smallest-string-with-a-given-numeric-value/README.md )
Original file line number Diff line number Diff line change
1
+ # 特殊数组的特征值
2
+
3
+ > 难度:简单
4
+ >
5
+ > https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/
6
+
7
+ ## 题目
8
+
9
+ 给你一个非负整数数组 ` nums ` 。如果存在一个数 ` x ` ,使得 ` nums ` 中恰好有 ` x ` 个元素 ** 大于或者等于** ` x ` ,那么就称 ` nums ` 是一个 ** 特殊数组** ,而 ` x ` 是该数组的 ** 特征值** 。
10
+
11
+ 注意: ` x ` 不必 是 ` nums ` 的中的元素。
12
+
13
+ 如果数组 ` nums ` 是一个 ** 特殊数组** ,请返回它的特征值 ` x ` 。否则,返回 ` -1 ` 。可以证明的是,如果 ` nums ` 是特殊数组,那么其特征值 ` x ` 是 ** 唯一的** 。
14
+
15
+ ### 示例
16
+
17
+ #### 示例 1:
18
+ ```
19
+ 输入:nums = [3,5]
20
+ 输出:2
21
+ 解释:有 2 个元素(3 和 5)大于或等于 2 。
22
+ ```
23
+
24
+ #### 示例 2:
25
+
26
+ ```
27
+ 输入:nums = [0,0]
28
+ 输出:-1
29
+ 解释:没有满足题目要求的特殊数组,故而也不存在特征值 x 。
30
+ 如果 x = 0,应该有 0 个元素 >= x,但实际有 2 个。
31
+ 如果 x = 1,应该有 1 个元素 >= x,但实际有 0 个。
32
+ 如果 x = 2,应该有 2 个元素 >= x,但实际有 0 个。
33
+ x 不能取更大的值,因为 nums 中只有两个元素。
34
+ ```
35
+
36
+ #### 示例 3:
37
+
38
+ ```
39
+ 输入:nums = [0,4,3,0,4]
40
+ 输出:3
41
+ 解释:有 3 个元素大于或等于 3 。
42
+ ```
43
+
44
+ #### 示例 4:
45
+
46
+ ```
47
+ 输入:nums = [3,6,7,7,0]
48
+ 输出:-1
49
+ ```
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { specialArray } from '.'
3
+
4
+ describe ( '特殊数组的特征值' , ( ) => {
5
+ testCase ( specialArray )
6
+ } )
7
+
8
+ function testCase ( fn : ( nums : number [ ] ) => number ) {
9
+ it . each ( [
10
+ [
11
+ [ 3 , 5 ] ,
12
+ 2 ,
13
+ ] ,
14
+ [
15
+ [ 0 , 0 ] ,
16
+ - 1 ,
17
+ ] ,
18
+ [
19
+ [ 0 , 4 , 3 , 0 , 4 ] ,
20
+ 3 ,
21
+ ] ,
22
+ [
23
+ [ 3 , 6 , 7 , 7 , 0 ] ,
24
+ - 1 ,
25
+ ] ,
26
+ ] ) ( '示例%#' , ( nums , expected ) => {
27
+ expect ( fn ( nums ) ) . toBe ( expected )
28
+ } )
29
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 降序排序 + 一次遍历
3
+ * @desc 时间复杂度 O(NlogN) 空间复杂度 O(logN)
4
+ * @param nums
5
+ * @returns
6
+ */
7
+ export function specialArray ( nums : number [ ] ) : number {
8
+ nums . sort ( ( a , b ) => a - b )
9
+ const n = nums . length
10
+ for ( let i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) {
11
+ const temp = nums [ i ]
12
+ nums [ i ] = nums [ j ]
13
+ nums [ j ] = temp
14
+ }
15
+ for ( let i = 1 ; i <= n ; ++ i ) {
16
+ if ( nums [ i - 1 ] >= i && ( i === n || nums [ i ] < i ) )
17
+ return i
18
+ }
19
+ return - 1
20
+ }
You can’t perform that action at this time.
0 commit comments