Skip to content

Commit 782d09e

Browse files
authored
Update 1608-special-array-with-x-elements-greater-than-or-equal-x.js
1 parent f99cfb0 commit 782d09e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

1608-special-array-with-x-elements-greater-than-or-equal-x.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,21 @@ const specialArray = function(nums) {
3030
// larger or equal to i, which makes array not special.
3131
return left < nums.length && left === nums[left] ? -1 : left
3232
};
33+
34+
// another
35+
36+
/**
37+
* @param {number[]} nums
38+
* @return {number}
39+
*/
40+
const specialArray = function(nums) {
41+
const n = nums.length
42+
nums.sort((a, b) => b - a)
43+
let l = 0, r = n
44+
while(l < r) {
45+
const mid = l + ((r - l) >> 1)
46+
if(nums[mid] > mid) l = mid + 1
47+
else r = mid
48+
}
49+
return l < n && l === nums[l] ? -1 : l
50+
}

0 commit comments

Comments
 (0)