Skip to content

Commit d9237bc

Browse files
committed
feat: add solutions to lc problem: No.1608
No.1608.Special Array With X Elements Greater Than or Equal X
1 parent e2ceae8 commit d9237bc

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed

solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,97 @@ func specialArray(nums []int) int {
220220
}
221221
```
222222

223+
### **TypeScript**
224+
225+
```ts
226+
function specialArray(nums: number[]): number {
227+
const n = nums.length;
228+
for (let i = 0; i <= n; i++) {
229+
if (i === nums.reduce((r, v) => r + (v >= i ? 1 : 0), 0)) {
230+
return i;
231+
}
232+
}
233+
return -1;
234+
}
235+
```
236+
237+
```ts
238+
function specialArray(nums: number[]): number {
239+
const n = nums.length;
240+
let left = 0;
241+
let right = n + 1;
242+
while (left < right) {
243+
const mid = (left + right) >> 1;
244+
const count = nums.reduce((r, v) => r + (v >= mid ? 1 : 0), 0);
245+
246+
if (count === mid) {
247+
return mid;
248+
}
249+
250+
if (count > mid) {
251+
left = mid + 1;
252+
} else {
253+
right = mid;
254+
}
255+
}
256+
return -1;
257+
}
258+
```
259+
260+
### **Rust**
261+
262+
```rust
263+
impl Solution {
264+
pub fn special_array(nums: Vec<i32>) -> i32 {
265+
let n = nums.len() as i32;
266+
for i in 0..=n {
267+
let mut count = 0;
268+
for &num in nums.iter() {
269+
if num >= i {
270+
count += 1;
271+
}
272+
}
273+
if count == i {
274+
return i;
275+
}
276+
}
277+
-1
278+
}
279+
}
280+
```
281+
282+
```rust
283+
use std::cmp::Ordering;
284+
impl Solution {
285+
pub fn special_array(nums: Vec<i32>) -> i32 {
286+
let n = nums.len() as i32;
287+
let mut left = 0;
288+
let mut right = n + 1;
289+
while left < right {
290+
let mid = left + (right - left) / 2;
291+
let mut count = 0;
292+
for &num in nums.iter() {
293+
if num >= mid {
294+
count += 1;
295+
}
296+
}
297+
match count.cmp(&mid) {
298+
Ordering::Equal => {
299+
return mid;
300+
}
301+
Ordering::Less => {
302+
right = mid;
303+
}
304+
Ordering::Greater => {
305+
left = mid + 1;
306+
}
307+
}
308+
}
309+
-1
310+
}
311+
}
312+
```
313+
223314
### **...**
224315

225316
```

solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,97 @@ func specialArray(nums []int) int {
194194
}
195195
```
196196

197+
### **TypeScript**
198+
199+
```ts
200+
function specialArray(nums: number[]): number {
201+
const n = nums.length;
202+
for (let i = 0; i <= n; i++) {
203+
if (i === nums.reduce((r, v) => r + (v >= i ? 1 : 0), 0)) {
204+
return i;
205+
}
206+
}
207+
return -1;
208+
}
209+
```
210+
211+
```ts
212+
function specialArray(nums: number[]): number {
213+
const n = nums.length;
214+
let left = 0;
215+
let right = n + 1;
216+
while (left < right) {
217+
const mid = (left + right) >> 1;
218+
const count = nums.reduce((r, v) => r + (v >= mid ? 1 : 0), 0);
219+
220+
if (count === mid) {
221+
return mid;
222+
}
223+
224+
if (count > mid) {
225+
left = mid + 1;
226+
} else {
227+
right = mid;
228+
}
229+
}
230+
return -1;
231+
}
232+
```
233+
234+
### **Rust**
235+
236+
```rust
237+
impl Solution {
238+
pub fn special_array(nums: Vec<i32>) -> i32 {
239+
let n = nums.len() as i32;
240+
for i in 0..=n {
241+
let mut count = 0;
242+
for &num in nums.iter() {
243+
if num >= i {
244+
count += 1;
245+
}
246+
}
247+
if count == i {
248+
return i;
249+
}
250+
}
251+
-1
252+
}
253+
}
254+
```
255+
256+
```rust
257+
use std::cmp::Ordering;
258+
impl Solution {
259+
pub fn special_array(nums: Vec<i32>) -> i32 {
260+
let n = nums.len() as i32;
261+
let mut left = 0;
262+
let mut right = n + 1;
263+
while left < right {
264+
let mid = left + (right - left) / 2;
265+
let mut count = 0;
266+
for &num in nums.iter() {
267+
if num >= mid {
268+
count += 1;
269+
}
270+
}
271+
match count.cmp(&mid) {
272+
Ordering::Equal => {
273+
return mid;
274+
}
275+
Ordering::Less => {
276+
right = mid;
277+
}
278+
Ordering::Greater => {
279+
left = mid + 1;
280+
}
281+
}
282+
}
283+
-1
284+
}
285+
}
286+
```
287+
197288
### **...**
198289

199290
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::cmp::Ordering;
2+
impl Solution {
3+
pub fn special_array(nums: Vec<i32>) -> i32 {
4+
let n = nums.len() as i32;
5+
let mut left = 0;
6+
let mut right = n + 1;
7+
while left < right {
8+
let mid = left + (right - left) / 2;
9+
let mut count = 0;
10+
for &num in nums.iter() {
11+
if num >= mid {
12+
count += 1;
13+
}
14+
}
15+
match count.cmp(&mid) {
16+
Ordering::Equal => {
17+
return mid;
18+
}
19+
Ordering::Less => {
20+
right = mid;
21+
}
22+
Ordering::Greater => {
23+
left = mid + 1;
24+
}
25+
}
26+
}
27+
-1
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function specialArray(nums: number[]): number {
2+
const n = nums.length;
3+
let left = 0;
4+
let right = n + 1;
5+
while (left < right) {
6+
const mid = (left + right) >> 1;
7+
const count = nums.reduce((r, v) => r + (v >= mid ? 1 : 0), 0);
8+
9+
if (count === mid) {
10+
return mid;
11+
}
12+
13+
if (count > mid) {
14+
left = mid + 1;
15+
} else {
16+
right = mid;
17+
}
18+
}
19+
return -1;
20+
}

0 commit comments

Comments
 (0)