Skip to content

Commit 89b9e4e

Browse files
committed
Add solution #1608
1 parent 6cc653d commit 89b9e4e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,416 LeetCode solutions in JavaScript
1+
# 1,417 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1240,6 +1240,7 @@
12401240
1603|[Design Parking System](./solutions/1603-design-parking-system.js)|Easy|
12411241
1604|[Alert Using Same Key-Card Three or More Times in a One Hour Period](./solutions/1604-alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.js)|Medium|
12421242
1605|[Find Valid Matrix Given Row and Column Sums](./solutions/1605-find-valid-matrix-given-row-and-column-sums.js)|Medium|
1243+
1608|[Special Array With X Elements Greater Than or Equal X](./solutions/1608-special-array-with-x-elements-greater-than-or-equal-x.js)|Easy|
12431244
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12441245
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12451246
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1608. Special Array With X Elements Greater Than or Equal X
3+
* https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array nums of non-negative integers. nums is considered special if there exists
7+
* a number x such that there are exactly x numbers in nums that are greater than or equal to x.
8+
*
9+
* Notice that x does not have to be an element in nums.
10+
*
11+
* Return x if the array is special, otherwise, return -1. It can be proven that if nums is special,
12+
* the value for x is unique.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @param {number} start
18+
* @param {number} end
19+
* @return {number}
20+
*/
21+
function specialArray(numbers) {
22+
numbers.sort((a, b) => b - a);
23+
let left = 0;
24+
let right = numbers.length;
25+
26+
while (left <= right) {
27+
const mid = Math.floor((left + right) / 2);
28+
if (mid === 0 || numbers[mid - 1] >= mid) {
29+
if (mid === numbers.length || numbers[mid] < mid) {
30+
return mid;
31+
}
32+
left = mid + 1;
33+
} else {
34+
right = mid - 1;
35+
}
36+
}
37+
38+
return -1;
39+
}

0 commit comments

Comments
 (0)