Skip to content

Commit 572c550

Browse files
committed
feat: add solutions to lc problems
* No.1608.Special Array With X Elements Greater Than or Equal X * No.1609.Even Odd Tree
1 parent dcb76b2 commit 572c550

File tree

12 files changed

+584
-194
lines changed

12 files changed

+584
-194
lines changed

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

+80-16
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
5757

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

60-
**方法一:排序 + 二分查找**
60+
**方法一:暴力枚举**
6161

62-
对 nums 进行排序
62+
在 $[1..n]$ 范围内枚举 $x$,然后统计数组中大于等于 $x$ 的元素个数,记为 $cnt$。若存在 $cnt$ 与 $x$ 相等,直接返回 $x$
6363

64-
接下来在 `[0, n]` 范围内遍历 x,判断 nums 中是否存在大于等于 x 的个数 cnt,使得 `cnt == x`。若存在,直接范围 x
64+
时间复杂度 $O(n^2)$
6565

66-
否则遍历结束返回 -1。
66+
**方法二:排序 + 二分查找**
67+
68+
我们也可以先对 `nums` 进行排序。
69+
70+
接下来同样枚举 $x$,利用二分查找,找到 `nums` 中第一个大于等于 $x$ 的元素,快速统计出 `nums` 中大于等于 $x$ 的元素个数。
71+
72+
时间复杂度 $O(n\log n)$。
6773

6874
<!-- tabs:start -->
6975

@@ -74,11 +80,20 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
7480
```python
7581
class Solution:
7682
def specialArray(self, nums: List[int]) -> int:
77-
n = len(nums)
83+
for x in range(1, len(nums) + 1):
84+
cnt = sum(v >= x for v in nums)
85+
if cnt == x:
86+
return x
87+
return -1
88+
```
89+
90+
```python
91+
class Solution:
92+
def specialArray(self, nums: List[int]) -> int:
7893
nums.sort()
79-
for x in range(n + 1):
80-
idx = bisect_left(nums, x)
81-
cnt = n - 1 - idx + 1
94+
n = len(nums)
95+
for x in range(1, n + 1):
96+
cnt = n - bisect_left(nums, x)
8297
if cnt == x:
8398
return x
8499
return -1
@@ -88,12 +103,31 @@ class Solution:
88103

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

106+
```java
107+
class Solution {
108+
public int specialArray(int[] nums) {
109+
for (int x = 1; x <= nums.length; ++x) {
110+
int cnt = 0;
111+
for (int v : nums) {
112+
if (v >= x) {
113+
++cnt;
114+
}
115+
}
116+
if (cnt == x) {
117+
return x;
118+
}
119+
}
120+
return -1;
121+
}
122+
}
123+
```
124+
91125
```java
92126
class Solution {
93127
public int specialArray(int[] nums) {
94128
Arrays.sort(nums);
95129
int n = nums.length;
96-
for (int x = 0; x <= n; ++x) {
130+
for (int x = 1; x <= n; ++x) {
97131
int left = 0, right = n;
98132
while (left < right) {
99133
int mid = (left + right) >> 1;
@@ -103,7 +137,7 @@ class Solution {
103137
left = mid + 1;
104138
}
105139
}
106-
int cnt = n - 1 - left + 1;
140+
int cnt = n - left;
107141
if (cnt == x) {
108142
return x;
109143
}
@@ -115,15 +149,28 @@ class Solution {
115149

116150
### **C++**
117151

152+
```cpp
153+
class Solution {
154+
public:
155+
int specialArray(vector<int>& nums) {
156+
for (int x = 1; x <= nums.size(); ++x) {
157+
int cnt = 0;
158+
for (int v : nums) cnt += v >= x;
159+
if (cnt == x) return x;
160+
}
161+
return -1;
162+
}
163+
};
164+
```
165+
118166
```cpp
119167
class Solution {
120168
public:
121169
int specialArray(vector<int>& nums) {
122170
int n = nums.size();
123171
sort(nums.begin(), nums.end());
124-
for (int x = 0; x <= n; ++x) {
125-
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
126-
int cnt = n - 1 - idx + 1;
172+
for (int x = 1; x <= n; ++x) {
173+
int cnt = n - (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
127174
if (cnt == x) return x;
128175
}
129176
return -1;
@@ -135,9 +182,26 @@ public:
135182

136183
```go
137184
func specialArray(nums []int) int {
138-
n := len(nums)
185+
for x := 1; x <= len(nums); x++ {
186+
cnt := 0
187+
for _, v := range nums {
188+
if v >= x {
189+
cnt++
190+
}
191+
}
192+
if cnt == x {
193+
return x
194+
}
195+
}
196+
return -1
197+
}
198+
```
199+
200+
```go
201+
func specialArray(nums []int) int {
139202
sort.Ints(nums)
140-
for x := 0; x <= n; x++ {
203+
n := len(nums)
204+
for x := 1; x <= n; x++ {
141205
left, right := 0, n
142206
for left < right {
143207
mid := (left + right) >> 1
@@ -147,7 +211,7 @@ func specialArray(nums []int) int {
147211
left = mid + 1
148212
}
149213
}
150-
cnt := n - 1 - left + 1
214+
cnt := n - left
151215
if cnt == x {
152216
return x
153217
}

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

+70-12
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,52 @@ x cannot be greater since there are only 2 numbers in nums.
5656
```python
5757
class Solution:
5858
def specialArray(self, nums: List[int]) -> int:
59-
n = len(nums)
59+
for x in range(1, len(nums) + 1):
60+
cnt = sum(v >= x for v in nums)
61+
if cnt == x:
62+
return x
63+
return -1
64+
```
65+
66+
```python
67+
class Solution:
68+
def specialArray(self, nums: List[int]) -> int:
6069
nums.sort()
61-
for x in range(n + 1):
62-
idx = bisect_left(nums, x)
63-
cnt = n - 1 - idx + 1
70+
n = len(nums)
71+
for x in range(1, n + 1):
72+
cnt = n - bisect_left(nums, x)
6473
if cnt == x:
6574
return x
6675
return -1
6776
```
6877

6978
### **Java**
7079

80+
```java
81+
class Solution {
82+
public int specialArray(int[] nums) {
83+
for (int x = 1; x <= nums.length; ++x) {
84+
int cnt = 0;
85+
for (int v : nums) {
86+
if (v >= x) {
87+
++cnt;
88+
}
89+
}
90+
if (cnt == x) {
91+
return x;
92+
}
93+
}
94+
return -1;
95+
}
96+
}
97+
```
98+
7199
```java
72100
class Solution {
73101
public int specialArray(int[] nums) {
74102
Arrays.sort(nums);
75103
int n = nums.length;
76-
for (int x = 0; x <= n; ++x) {
104+
for (int x = 1; x <= n; ++x) {
77105
int left = 0, right = n;
78106
while (left < right) {
79107
int mid = (left + right) >> 1;
@@ -83,7 +111,7 @@ class Solution {
83111
left = mid + 1;
84112
}
85113
}
86-
int cnt = n - 1 - left + 1;
114+
int cnt = n - left;
87115
if (cnt == x) {
88116
return x;
89117
}
@@ -95,15 +123,28 @@ class Solution {
95123

96124
### **C++**
97125

126+
```cpp
127+
class Solution {
128+
public:
129+
int specialArray(vector<int>& nums) {
130+
for (int x = 1; x <= nums.size(); ++x) {
131+
int cnt = 0;
132+
for (int v : nums) cnt += v >= x;
133+
if (cnt == x) return x;
134+
}
135+
return -1;
136+
}
137+
};
138+
```
139+
98140
```cpp
99141
class Solution {
100142
public:
101143
int specialArray(vector<int>& nums) {
102144
int n = nums.size();
103145
sort(nums.begin(), nums.end());
104-
for (int x = 0; x <= n; ++x) {
105-
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
106-
int cnt = n - 1 - idx + 1;
146+
for (int x = 1; x <= n; ++x) {
147+
int cnt = n - (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
107148
if (cnt == x) return x;
108149
}
109150
return -1;
@@ -115,9 +156,26 @@ public:
115156

116157
```go
117158
func specialArray(nums []int) int {
118-
n := len(nums)
159+
for x := 1; x <= len(nums); x++ {
160+
cnt := 0
161+
for _, v := range nums {
162+
if v >= x {
163+
cnt++
164+
}
165+
}
166+
if cnt == x {
167+
return x
168+
}
169+
}
170+
return -1
171+
}
172+
```
173+
174+
```go
175+
func specialArray(nums []int) int {
119176
sort.Ints(nums)
120-
for x := 0; x <= n; x++ {
177+
n := len(nums)
178+
for x := 1; x <= n; x++ {
121179
left, right := 0, n
122180
for left < right {
123181
mid := (left + right) >> 1
@@ -127,7 +185,7 @@ func specialArray(nums []int) int {
127185
left = mid + 1
128186
}
129187
}
130-
cnt := n - 1 - left + 1
188+
cnt := n - left
131189
if cnt == x {
132190
return x
133191
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ class Solution {
33
int specialArray(vector<int>& nums) {
44
int n = nums.size();
55
sort(nums.begin(), nums.end());
6-
for (int x = 0; x <= n; ++x) {
7-
int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
8-
int cnt = n - 1 - idx + 1;
6+
for (int x = 1; x <= n; ++x) {
7+
int cnt = n - (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
98
if (cnt == x) return x;
109
}
1110
return -1;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
func specialArray(nums []int) int {
2-
n := len(nums)
32
sort.Ints(nums)
4-
for x := 0; x <= n; x++ {
3+
n := len(nums)
4+
for x := 1; x <= n; x++ {
55
left, right := 0, n
66
for left < right {
77
mid := (left + right) >> 1
@@ -11,7 +11,7 @@ func specialArray(nums []int) int {
1111
left = mid + 1
1212
}
1313
}
14-
cnt := n - 1 - left + 1
14+
cnt := n - left
1515
if cnt == x {
1616
return x
1717
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public int specialArray(int[] nums) {
33
Arrays.sort(nums);
44
int n = nums.length;
5-
for (int x = 0; x <= n; ++x) {
5+
for (int x = 1; x <= n; ++x) {
66
int left = 0, right = n;
77
while (left < right) {
88
int mid = (left + right) >> 1;
@@ -12,7 +12,7 @@ public int specialArray(int[] nums) {
1212
left = mid + 1;
1313
}
1414
}
15-
int cnt = n - 1 - left + 1;
15+
int cnt = n - left;
1616
if (cnt == x) {
1717
return x;
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def specialArray(self, nums: List[int]) -> int:
3-
n = len(nums)
43
nums.sort()
5-
for x in range(n + 1):
6-
idx = bisect_left(nums, x)
7-
cnt = n - 1 - idx + 1
4+
n = len(nums)
5+
for x in range(1, n + 1):
6+
cnt = n - bisect_left(nums, x)
87
if cnt == x:
98
return x
109
return -1

0 commit comments

Comments
 (0)