Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f769d96

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 79488d3 commit f769d96

File tree

6 files changed

+224
-2
lines changed

6 files changed

+224
-2
lines changed

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,104 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:排序 + 二分查找**
61+
62+
对 nums 进行排序。
63+
64+
接下来在 `[0, n]` 范围内遍历 x,判断 nums 中是否存在大于等于 x 的个数 cnt,使得 `cnt == x`。若存在,直接范围 x。
65+
66+
否则遍历结束返回 -1。
67+
6068
<!-- tabs:start -->
6169

6270
### **Python3**
6371

6472
<!-- 这里可写当前语言的特殊实现逻辑 -->
6573

6674
```python
67-
75+
class Solution:
76+
def specialArray(self, nums: List[int]) -> int:
77+
n = len(nums)
78+
nums.sort()
79+
for x in range(n + 1):
80+
idx = bisect_left(nums, x)
81+
cnt = n - 1 - idx + 1
82+
if cnt == x:
83+
return x
84+
return -1
6885
```
6986

7087
### **Java**
7188

7289
<!-- 这里可写当前语言的特殊实现逻辑 -->
7390

7491
```java
92+
class Solution {
93+
public int specialArray(int[] nums) {
94+
Arrays.sort(nums);
95+
int n = nums.length;
96+
for (int x = 0; x <= n; ++x) {
97+
int left = 0, right = n;
98+
while (left < right) {
99+
int mid = (left + right) >> 1;
100+
if (nums[mid] >= x) {
101+
right = mid;
102+
} else {
103+
left = mid + 1;
104+
}
105+
}
106+
int cnt = n - 1 - left + 1;
107+
if (cnt == x) {
108+
return x;
109+
}
110+
}
111+
return -1;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int specialArray(vector<int>& nums) {
122+
int n = nums.size();
123+
sort(nums.begin(), nums.end());
124+
for (int x = 0; x <= n; ++x)
125+
{
126+
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
127+
int cnt = n - 1 - idx + 1;
128+
if (cnt == x) return x;
129+
}
130+
return -1;
131+
}
132+
};
133+
```
75134
135+
### **Go**
136+
137+
```go
138+
func specialArray(nums []int) int {
139+
n := len(nums)
140+
sort.Ints(nums)
141+
for x := 0; x <= n; x++ {
142+
left, right := 0, n
143+
for left < right {
144+
mid := (left + right) >> 1
145+
if nums[mid] >= x {
146+
right = mid
147+
} else {
148+
left = mid + 1
149+
}
150+
}
151+
cnt := n - 1 - left + 1
152+
if cnt == x {
153+
return x
154+
}
155+
}
156+
return -1
157+
}
76158
```
77159

78160
### **...**

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,87 @@ x cannot be greater since there are only 2 numbers in nums.
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def specialArray(self, nums: List[int]) -> int:
59+
n = len(nums)
60+
nums.sort()
61+
for x in range(n + 1):
62+
idx = bisect_left(nums, x)
63+
cnt = n - 1 - idx + 1
64+
if cnt == x:
65+
return x
66+
return -1
5867
```
5968

6069
### **Java**
6170

6271
```java
72+
class Solution {
73+
public int specialArray(int[] nums) {
74+
Arrays.sort(nums);
75+
int n = nums.length;
76+
for (int x = 0; x <= n; ++x) {
77+
int left = 0, right = n;
78+
while (left < right) {
79+
int mid = (left + right) >> 1;
80+
if (nums[mid] >= x) {
81+
right = mid;
82+
} else {
83+
left = mid + 1;
84+
}
85+
}
86+
int cnt = n - 1 - left + 1;
87+
if (cnt == x) {
88+
return x;
89+
}
90+
}
91+
return -1;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int specialArray(vector<int>& nums) {
102+
int n = nums.size();
103+
sort(nums.begin(), nums.end());
104+
for (int x = 0; x <= n; ++x)
105+
{
106+
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
107+
int cnt = n - 1 - idx + 1;
108+
if (cnt == x) return x;
109+
}
110+
return -1;
111+
}
112+
};
113+
```
63114
115+
### **Go**
116+
117+
```go
118+
func specialArray(nums []int) int {
119+
n := len(nums)
120+
sort.Ints(nums)
121+
for x := 0; x <= n; x++ {
122+
left, right := 0, n
123+
for left < right {
124+
mid := (left + right) >> 1
125+
if nums[mid] >= x {
126+
right = mid
127+
} else {
128+
left = mid + 1
129+
}
130+
}
131+
cnt := n - 1 - left + 1
132+
if cnt == x {
133+
return x
134+
}
135+
}
136+
return -1
137+
}
64138
```
65139

66140
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int specialArray(vector<int>& nums) {
4+
int n = nums.size();
5+
sort(nums.begin(), nums.end());
6+
for (int x = 0; x <= n; ++x)
7+
{
8+
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
9+
int cnt = n - 1 - idx + 1;
10+
if (cnt == x) return x;
11+
}
12+
return -1;
13+
}
14+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func specialArray(nums []int) int {
2+
n := len(nums)
3+
sort.Ints(nums)
4+
for x := 0; x <= n; x++ {
5+
left, right := 0, n
6+
for left < right {
7+
mid := (left + right) >> 1
8+
if nums[mid] >= x {
9+
right = mid
10+
} else {
11+
left = mid + 1
12+
}
13+
}
14+
cnt := n - 1 - left + 1
15+
if cnt == x {
16+
return x
17+
}
18+
}
19+
return -1
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int specialArray(int[] nums) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
for (int x = 0; x <= n; ++x) {
6+
int left = 0, right = n;
7+
while (left < right) {
8+
int mid = (left + right) >> 1;
9+
if (nums[mid] >= x) {
10+
right = mid;
11+
} else {
12+
left = mid + 1;
13+
}
14+
}
15+
int cnt = n - 1 - left + 1;
16+
if (cnt == x) {
17+
return x;
18+
}
19+
}
20+
return -1;
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def specialArray(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
nums.sort()
5+
for x in range(n + 1):
6+
idx = bisect_left(nums, x)
7+
cnt = n - 1 - idx + 1
8+
if cnt == x:
9+
return x
10+
return -1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /