Skip to content

Commit 918662e

Browse files
committed
feat: add solutions to lc problem: No.1394
No.1394.Find Lucky Integer in an Array
1 parent d4b3ea3 commit 918662e

File tree

9 files changed

+141
-122
lines changed

9 files changed

+141
-122
lines changed

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

Lines changed: 49 additions & 41 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,29 +93,24 @@ 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
104100

105101
```java
106102
class Solution {
107103
public int findLucky(int[] arr) {
108-
int[] cnt = new int[510];
109-
for (int x : cnt) {
104+
int[] cnt = new int[501];
105+
for (int x : arr) {
110106
++cnt[x];
111107
}
112-
int ans = -1;
113-
for (int x = 1; x < cnt.length; ++x) {
114-
if (cnt[x] == x) {
115-
ans = x;
108+
for (int x = cnt.length - 1; x > 0; --x) {
109+
if (x == cnt[x]) {
110+
return x;
116111
}
117112
}
118-
return ans;
113+
return -1;
119114
}
120115
}
121116
```
@@ -126,18 +121,16 @@ class Solution {
126121
class Solution {
127122
public:
128123
int findLucky(vector<int>& arr) {
129-
int cnt[510];
130-
memset(cnt, 0, sizeof(cnt));
124+
int cnt[501]{};
131125
for (int x : arr) {
132126
++cnt[x];
133127
}
134-
int ans = -1;
135-
for (int x = 1; x < 510; ++x) {
136-
if (cnt[x] == x) {
137-
ans = x;
128+
for (int x = 500; x; --x) {
129+
if (x == cnt[x]) {
130+
return x;
138131
}
139132
}
140-
return ans;
133+
return -1;
141134
}
142135
};
143136
```
@@ -146,35 +139,51 @@ public:
146139
147140
```go
148141
func findLucky(arr []int) int {
149-
cnt := [510]int{}
142+
cnt := [501]int{}
150143
for _, x := range arr {
151144
cnt[x]++
152145
}
153-
ans := -1
154-
for x := 1; x < len(cnt); x++ {
155-
if cnt[x] == x {
156-
ans = x
146+
for x := len(cnt) - 1; x > 0; x-- {
147+
if x == cnt[x] {
148+
return x
157149
}
158150
}
159-
return ans
151+
return -1
160152
}
161153
```
162154

163155
#### TypeScript
164156

165157
```ts
166158
function findLucky(arr: number[]): number {
167-
const cnt = Array(510).fill(0);
159+
const cnt: number[] = Array(501).fill(0);
168160
for (const x of arr) {
169161
++cnt[x];
170162
}
171-
let ans = -1;
172-
for (let x = 1; x < cnt.length; ++x) {
173-
if (cnt[x] === x) {
174-
ans = x;
163+
for (let x = cnt.length - 1; x; --x) {
164+
if (x === cnt[x]) {
165+
return x;
175166
}
176167
}
177-
return ans;
168+
return -1;
169+
}
170+
```
171+
172+
#### Rust
173+
174+
```rust
175+
use std::collections::HashMap;
176+
177+
impl Solution {
178+
pub fn find_lucky(arr: Vec<i32>) -> i32 {
179+
let mut cnt = HashMap::new();
180+
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
181+
cnt.iter()
182+
.filter(|(&x, &v)| x == v)
183+
.map(|(&x, _)| x)
184+
.max()
185+
.unwrap_or(-1)
186+
}
178187
}
179188
```
180189

@@ -187,17 +196,16 @@ class Solution {
187196
* @return Integer
188197
*/
189198
function findLucky($arr) {
190-
$max = -1;
191-
for ($i = 0; $i < count($arr); $i++) {
192-
$hashtable[$arr[$i]] += 1;
199+
$cnt = array_fill(0, 501, 0);
200+
foreach ($arr as $x) {
201+
$cnt[$x]++;
193202
}
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]);
203+
for ($x = 500; $x > 0; $x--) {
204+
if ($cnt[$x] === $x) {
205+
return $x;
198206
}
199207
}
200-
return $max;
208+
return -1;
201209
}
202210
}
203211
```

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

Lines changed: 49 additions & 41 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,29 +77,24 @@ 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
8884

8985
```java
9086
class Solution {
9187
public int findLucky(int[] arr) {
92-
int[] cnt = new int[510];
93-
for (int x : cnt) {
88+
int[] cnt = new int[501];
89+
for (int x : arr) {
9490
++cnt[x];
9591
}
96-
int ans = -1;
97-
for (int x = 1; x < cnt.length; ++x) {
98-
if (cnt[x] == x) {
99-
ans = x;
92+
for (int x = cnt.length - 1; x > 0; --x) {
93+
if (x == cnt[x]) {
94+
return x;
10095
}
10196
}
102-
return ans;
97+
return -1;
10398
}
10499
}
105100
```
@@ -110,18 +105,16 @@ class Solution {
110105
class Solution {
111106
public:
112107
int findLucky(vector<int>& arr) {
113-
int cnt[510];
114-
memset(cnt, 0, sizeof(cnt));
108+
int cnt[501]{};
115109
for (int x : arr) {
116110
++cnt[x];
117111
}
118-
int ans = -1;
119-
for (int x = 1; x < 510; ++x) {
120-
if (cnt[x] == x) {
121-
ans = x;
112+
for (int x = 500; x; --x) {
113+
if (x == cnt[x]) {
114+
return x;
122115
}
123116
}
124-
return ans;
117+
return -1;
125118
}
126119
};
127120
```
@@ -130,35 +123,51 @@ public:
130123
131124
```go
132125
func findLucky(arr []int) int {
133-
cnt := [510]int{}
126+
cnt := [501]int{}
134127
for _, x := range arr {
135128
cnt[x]++
136129
}
137-
ans := -1
138-
for x := 1; x < len(cnt); x++ {
139-
if cnt[x] == x {
140-
ans = x
130+
for x := len(cnt) - 1; x > 0; x-- {
131+
if x == cnt[x] {
132+
return x
141133
}
142134
}
143-
return ans
135+
return -1
144136
}
145137
```
146138

147139
#### TypeScript
148140

149141
```ts
150142
function findLucky(arr: number[]): number {
151-
const cnt = Array(510).fill(0);
143+
const cnt: number[] = Array(501).fill(0);
152144
for (const x of arr) {
153145
++cnt[x];
154146
}
155-
let ans = -1;
156-
for (let x = 1; x < cnt.length; ++x) {
157-
if (cnt[x] === x) {
158-
ans = x;
147+
for (let x = cnt.length - 1; x; --x) {
148+
if (x === cnt[x]) {
149+
return x;
159150
}
160151
}
161-
return ans;
152+
return -1;
153+
}
154+
```
155+
156+
#### Rust
157+
158+
```rust
159+
use std::collections::HashMap;
160+
161+
impl Solution {
162+
pub fn find_lucky(arr: Vec<i32>) -> i32 {
163+
let mut cnt = HashMap::new();
164+
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
165+
cnt.iter()
166+
.filter(|(&x, &v)| x == v)
167+
.map(|(&x, _)| x)
168+
.max()
169+
.unwrap_or(-1)
170+
}
162171
}
163172
```
164173

@@ -171,17 +180,16 @@ class Solution {
171180
* @return Integer
172181
*/
173182
function findLucky($arr) {
174-
$max = -1;
175-
for ($i = 0; $i < count($arr); $i++) {
176-
$hashtable[$arr[$i]] += 1;
183+
$cnt = array_fill(0, 501, 0);
184+
foreach ($arr as $x) {
185+
$cnt[$x]++;
177186
}
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]);
187+
for ($x = 500; $x > 0; $x--) {
188+
if ($cnt[$x] === $x) {
189+
return $x;
182190
}
183191
}
184-
return $max;
192+
return -1;
185193
}
186194
}
187195
```
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
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution {
22
public int findLucky(int[] arr) {
3-
int[] cnt = new int[510];
4-
for (int x : cnt) {
3+
int[] cnt = new int[501];
4+
for (int x : arr) {
55
++cnt[x];
66
}
7-
int ans = -1;
8-
for (int x = 1; x < cnt.length; ++x) {
9-
if (cnt[x] == x) {
10-
ans = x;
7+
for (int x = cnt.length - 1; x > 0; --x) {
8+
if (x == cnt[x]) {
9+
return x;
1110
}
1211
}
13-
return ans;
12+
return -1;
1413
}
1514
}

0 commit comments

Comments
 (0)