Skip to content

Commit 4642438

Browse files
Merge branch 'main' into done1394
2 parents 8e01f99 + b3ce308 commit 4642438

File tree

29 files changed

+1194
-145
lines changed

29 files changed

+1194
-145
lines changed

solution/1300-1399/1394.Find Lucky Integer in an Array/README.md

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ tags:
8181

8282
### 方法一:计数
8383

84-
我们可以用哈希表或数组 $cnt$ 统计 $arr$ 中每个数字出现的次数,然后遍历 $cnt$,找到满足 $cnt[x] = x$ 的最大的 $x$ 即可。如果没有这样的 $x$,则返回 $-1$。
84+
我们可以用哈希表或数组 $\textit{cnt}$ 统计 $\textit{arr}$ 中每个数字出现的次数,然后遍历 $\textit{cnt}$,找到满足 $\textit{cnt}[x] = x$ 的最大的 $x$ 即可。如果没有这样的 $x$,则返回 $-1$。
8585

86-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $arr$ 的长度。
86+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $\textit{arr}$ 的长度。
8787

8888
<!-- tabs:start -->
8989

@@ -93,11 +93,7 @@ tags:
9393
class Solution:
9494
def findLucky(self, arr: List[int]) -> int:
9595
cnt = Counter(arr)
96-
ans = -1
97-
for x, v in cnt.items():
98-
if x == v and ans < x:
99-
ans = x
100-
return ans
96+
return max((x for x, v in cnt.items() if x == v), default=-1)
10197
```
10298

10399
#### Java
@@ -126,18 +122,16 @@ class Solution {
126122
class Solution {
127123
public:
128124
int findLucky(vector<int>& arr) {
129-
int cnt[510];
130-
memset(cnt, 0, sizeof(cnt));
125+
int cnt[501]{};
131126
for (int x : arr) {
132127
++cnt[x];
133128
}
134-
int ans = -1;
135-
for (int x = 1; x < 510; ++x) {
136-
if (cnt[x] == x) {
137-
ans = x;
129+
for (int x = 500; x; --x) {
130+
if (x == cnt[x]) {
131+
return x;
138132
}
139133
}
140-
return ans;
134+
return -1;
141135
}
142136
};
143137
```
@@ -146,35 +140,51 @@ public:
146140
147141
```go
148142
func findLucky(arr []int) int {
149-
cnt := [510]int{}
143+
cnt := [501]int{}
150144
for _, x := range arr {
151145
cnt[x]++
152146
}
153-
ans := -1
154-
for x := 1; x < len(cnt); x++ {
155-
if cnt[x] == x {
156-
ans = x
147+
for x := len(cnt) - 1; x > 0; x-- {
148+
if x == cnt[x] {
149+
return x
157150
}
158151
}
159-
return ans
152+
return -1
160153
}
161154
```
162155

163156
#### TypeScript
164157

165158
```ts
166159
function findLucky(arr: number[]): number {
167-
const cnt = Array(510).fill(0);
160+
const cnt: number[] = Array(501).fill(0);
168161
for (const x of arr) {
169162
++cnt[x];
170163
}
171-
let ans = -1;
172-
for (let x = 1; x < cnt.length; ++x) {
173-
if (cnt[x] === x) {
174-
ans = x;
164+
for (let x = cnt.length - 1; x; --x) {
165+
if (x === cnt[x]) {
166+
return x;
175167
}
176168
}
177-
return ans;
169+
return -1;
170+
}
171+
```
172+
173+
#### Rust
174+
175+
```rust
176+
use std::collections::HashMap;
177+
178+
impl Solution {
179+
pub fn find_lucky(arr: Vec<i32>) -> i32 {
180+
let mut cnt = HashMap::new();
181+
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
182+
cnt.iter()
183+
.filter(|(&x, &v)| x == v)
184+
.map(|(&x, _)| x)
185+
.max()
186+
.unwrap_or(-1)
187+
}
178188
}
179189
```
180190

@@ -187,17 +197,16 @@ class Solution {
187197
* @return Integer
188198
*/
189199
function findLucky($arr) {
190-
$max = -1;
191-
for ($i = 0; $i < count($arr); $i++) {
192-
$hashtable[$arr[$i]] += 1;
200+
$cnt = array_fill(0, 501, 0);
201+
foreach ($arr as $x) {
202+
$cnt[$x]++;
193203
}
194-
$keys = array_keys($hashtable);
195-
for ($j = 0; $j < count($keys); $j++) {
196-
if ($hashtable[$keys[$j]] == $keys[$j]) {
197-
$max = max($max, $keys[$j]);
204+
for ($x = 500; $x > 0; $x--) {
205+
if ($cnt[$x] === $x) {
206+
return $x;
198207
}
199208
}
200-
return $max;
209+
return -1;
201210
}
202211
}
203212
```

solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ tags:
6565

6666
### Solution 1: Counting
6767

68-
We can use a hash table or array $cnt$ to count the occurrences of each number in $arr$, then traverse $cnt$ to find the largest $x$ that satisfies $cnt[x] = x$. If there is no such $x$, return $-1$.
68+
We can use a hash table or an array $\textit{cnt}$ to count the occurrences of each number in $\textit{arr}$. Then, we iterate through $\textit{cnt}$ to find the largest $x$ such that $\textit{cnt}[x] = x$. If there is no such $x$, return $-1$.
6969

70-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of $arr$.
70+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $\textit{arr}$.
7171

7272
<!-- tabs:start -->
7373

@@ -77,11 +77,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is
7777
class Solution:
7878
def findLucky(self, arr: List[int]) -> int:
7979
cnt = Counter(arr)
80-
ans = -1
81-
for x, v in cnt.items():
82-
if x == v and ans < x:
83-
ans = x
84-
return ans
80+
return max((x for x, v in cnt.items() if x == v), default=-1)
8581
```
8682

8783
#### Java
@@ -110,18 +106,16 @@ class Solution {
110106
class Solution {
111107
public:
112108
int findLucky(vector<int>& arr) {
113-
int cnt[510];
114-
memset(cnt, 0, sizeof(cnt));
109+
int cnt[501]{};
115110
for (int x : arr) {
116111
++cnt[x];
117112
}
118-
int ans = -1;
119-
for (int x = 1; x < 510; ++x) {
120-
if (cnt[x] == x) {
121-
ans = x;
113+
for (int x = 500; x; --x) {
114+
if (x == cnt[x]) {
115+
return x;
122116
}
123117
}
124-
return ans;
118+
return -1;
125119
}
126120
};
127121
```
@@ -130,35 +124,51 @@ public:
130124
131125
```go
132126
func findLucky(arr []int) int {
133-
cnt := [510]int{}
127+
cnt := [501]int{}
134128
for _, x := range arr {
135129
cnt[x]++
136130
}
137-
ans := -1
138-
for x := 1; x < len(cnt); x++ {
139-
if cnt[x] == x {
140-
ans = x
131+
for x := len(cnt) - 1; x > 0; x-- {
132+
if x == cnt[x] {
133+
return x
141134
}
142135
}
143-
return ans
136+
return -1
144137
}
145138
```
146139

147140
#### TypeScript
148141

149142
```ts
150143
function findLucky(arr: number[]): number {
151-
const cnt = Array(510).fill(0);
144+
const cnt: number[] = Array(501).fill(0);
152145
for (const x of arr) {
153146
++cnt[x];
154147
}
155-
let ans = -1;
156-
for (let x = 1; x < cnt.length; ++x) {
157-
if (cnt[x] === x) {
158-
ans = x;
148+
for (let x = cnt.length - 1; x; --x) {
149+
if (x === cnt[x]) {
150+
return x;
159151
}
160152
}
161-
return ans;
153+
return -1;
154+
}
155+
```
156+
157+
#### Rust
158+
159+
```rust
160+
use std::collections::HashMap;
161+
162+
impl Solution {
163+
pub fn find_lucky(arr: Vec<i32>) -> i32 {
164+
let mut cnt = HashMap::new();
165+
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
166+
cnt.iter()
167+
.filter(|(&x, &v)| x == v)
168+
.map(|(&x, _)| x)
169+
.max()
170+
.unwrap_or(-1)
171+
}
162172
}
163173
```
164174

@@ -171,17 +181,16 @@ class Solution {
171181
* @return Integer
172182
*/
173183
function findLucky($arr) {
174-
$max = -1;
175-
for ($i = 0; $i < count($arr); $i++) {
176-
$hashtable[$arr[$i]] += 1;
184+
$cnt = array_fill(0, 501, 0);
185+
foreach ($arr as $x) {
186+
$cnt[$x]++;
177187
}
178-
$keys = array_keys($hashtable);
179-
for ($j = 0; $j < count($keys); $j++) {
180-
if ($hashtable[$keys[$j]] == $keys[$j]) {
181-
$max = max($max, $keys[$j]);
188+
for ($x = 500; $x > 0; $x--) {
189+
if ($cnt[$x] === $x) {
190+
return $x;
182191
}
183192
}
184-
return $max;
193+
return -1;
185194
}
186195
}
187196
```
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public:
33
int findLucky(vector<int>& arr) {
4-
int cnt[510];
5-
memset(cnt, 0, sizeof(cnt));
4+
int cnt[501]{};
65
for (int x : arr) {
76
++cnt[x];
87
}
9-
int ans = -1;
10-
for (int x = 1; x < 510; ++x) {
11-
if (cnt[x] == x) {
12-
ans = x;
8+
for (int x = 500; x; --x) {
9+
if (x == cnt[x]) {
10+
return x;
1311
}
1412
}
15-
return ans;
13+
return -1;
1614
}
1715
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
func findLucky(arr []int) int {
2-
cnt := [510]int{}
2+
cnt := [501]int{}
33
for _, x := range arr {
44
cnt[x]++
55
}
6-
ans := -1
7-
for x := 1; x < len(cnt); x++ {
8-
if cnt[x] == x {
9-
ans = x
6+
for x := len(cnt) - 1; x > 0; x-- {
7+
if x == cnt[x] {
8+
return x
109
}
1110
}
12-
return ans
11+
return -1
1312
}

solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ class Solution {
44
* @return Integer
55
*/
66
function findLucky($arr) {
7-
$max = -1;
8-
for ($i = 0; $i < count($arr); $i++) {
9-
$hashtable[$arr[$i]] += 1;
7+
$cnt = array_fill(0, 501, 0);
8+
foreach ($arr as $x) {
9+
$cnt[$x]++;
1010
}
11-
$keys = array_keys($hashtable);
12-
for ($j = 0; $j < count($keys); $j++) {
13-
if ($hashtable[$keys[$j]] == $keys[$j]) {
14-
$max = max($max, $keys[$j]);
11+
for ($x = 500; $x > 0; $x--) {
12+
if ($cnt[$x] === $x) {
13+
return $x;
1514
}
1615
}
17-
return $max;
16+
return -1;
1817
}
19-
}
18+
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
class Solution:
22
def findLucky(self, arr: List[int]) -> int:
33
cnt = Counter(arr)
4-
ans = -1
5-
for x, v in cnt.items():
6-
if x == v and ans < x:
7-
ans = x
8-
return ans
4+
return max((x for x, v in cnt.items() if x == v), default=-1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn find_lucky(arr: Vec<i32>) -> i32 {
5+
let mut cnt = HashMap::new();
6+
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
7+
cnt.iter()
8+
.filter(|(&x, &v)| x == v)
9+
.map(|(&x, _)| x)
10+
.max()
11+
.unwrap_or(-1)
12+
}
13+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function findLucky(arr: number[]): number {
2-
const cnt = Array(510).fill(0);
2+
const cnt: number[] = Array(501).fill(0);
33
for (const x of arr) {
44
++cnt[x];
55
}
6-
let ans = -1;
7-
for (let x = 1; x < cnt.length; ++x) {
8-
if (cnt[x] === x) {
9-
ans = x;
6+
for (let x = cnt.length - 1; x; --x) {
7+
if (x === cnt[x]) {
8+
return x;
109
}
1110
}
12-
return ans;
11+
return -1;
1312
}

0 commit comments

Comments
 (0)