Skip to content

Commit 34e0642

Browse files
authored
Merge pull request neetcode-gh#3565 from Tetsuya3850/patch-19
Create 1608-special-array-with-x-elements-greater-than-or-equal-x.java
2 parents b32f7d5 + 2afbdb5 commit 34e0642

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int specialArray(int[] nums) {
3+
int[] count = new int[nums.length + 1];
4+
for (int n : nums) {
5+
int index = n < nums.length ? n : nums.length;
6+
count[index] += 1;
7+
}
8+
9+
int totalRight = 0;
10+
for (int i = nums.length; i >= 0; i--) {
11+
totalRight += count[i];
12+
if (i == totalRight) {
13+
return totalRight;
14+
}
15+
}
16+
17+
return -1;
18+
}
19+
}

0 commit comments

Comments
 (0)