Skip to content

feat: add solutions to lc problem: No.1394 #4547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 49 additions & 41 deletions solution/1300-1399/1394.Find Lucky Integer in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ tags:

### 方法一:计数

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

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

<!-- tabs:start -->

Expand All @@ -93,29 +93,24 @@ tags:
class Solution:
def findLucky(self, arr: List[int]) -> int:
cnt = Counter(arr)
ans = -1
for x, v in cnt.items():
if x == v and ans < x:
ans = x
return ans
return max((x for x, v in cnt.items() if x == v), default=-1)
```

#### Java

```java
class Solution {
public int findLucky(int[] arr) {
int[] cnt = new int[510];
for (int x : cnt) {
int[] cnt = new int[501];
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < cnt.length; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = cnt.length - 1; x > 0; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
}
```
Expand All @@ -126,18 +121,16 @@ class Solution {
class Solution {
public:
int findLucky(vector<int>& arr) {
int cnt[510];
memset(cnt, 0, sizeof(cnt));
int cnt[501]{};
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < 510; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = 500; x; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
};
```
Expand All @@ -146,35 +139,51 @@ public:

```go
func findLucky(arr []int) int {
cnt := [510]int{}
cnt := [501]int{}
for _, x := range arr {
cnt[x]++
}
ans := -1
for x := 1; x < len(cnt); x++ {
if cnt[x] == x {
ans = x
for x := len(cnt) - 1; x > 0; x-- {
if x == cnt[x] {
return x
}
}
return ans
return -1
}
```

#### TypeScript

```ts
function findLucky(arr: number[]): number {
const cnt = Array(510).fill(0);
const cnt: number[] = Array(501).fill(0);
for (const x of arr) {
++cnt[x];
}
let ans = -1;
for (let x = 1; x < cnt.length; ++x) {
if (cnt[x] === x) {
ans = x;
for (let x = cnt.length - 1; x; --x) {
if (x === cnt[x]) {
return x;
}
}
return ans;
return -1;
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_lucky(arr: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
cnt.iter()
.filter(|(&x, &v)| x == v)
.map(|(&x, _)| x)
.max()
.unwrap_or(-1)
}
}
```

Expand All @@ -187,17 +196,16 @@ class Solution {
* @return Integer
*/
function findLucky($arr) {
$max = -1;
for ($i = 0; $i < count($arr); $i++) {
$hashtable[$arr[$i]] += 1;
$cnt = array_fill(0, 501, 0);
foreach ($arr as $x) {
$cnt[$x]++;
}
$keys = array_keys($hashtable);
for ($j = 0; $j < count($keys); $j++) {
if ($hashtable[$keys[$j]] == $keys[$j]) {
$max = max($max, $keys[$j]);
for ($x = 500; $x > 0; $x--) {
if ($cnt[$x] === $x) {
return $x;
}
}
return $max;
return -1;
}
}
```
Expand Down
90 changes: 49 additions & 41 deletions solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ tags:

### Solution 1: Counting

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$.
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$.

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

<!-- tabs:start -->

Expand All @@ -77,29 +77,24 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is
class Solution:
def findLucky(self, arr: List[int]) -> int:
cnt = Counter(arr)
ans = -1
for x, v in cnt.items():
if x == v and ans < x:
ans = x
return ans
return max((x for x, v in cnt.items() if x == v), default=-1)
```

#### Java

```java
class Solution {
public int findLucky(int[] arr) {
int[] cnt = new int[510];
for (int x : cnt) {
int[] cnt = new int[501];
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < cnt.length; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = cnt.length - 1; x > 0; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
}
```
Expand All @@ -110,18 +105,16 @@ class Solution {
class Solution {
public:
int findLucky(vector<int>& arr) {
int cnt[510];
memset(cnt, 0, sizeof(cnt));
int cnt[501]{};
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < 510; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = 500; x; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
};
```
Expand All @@ -130,35 +123,51 @@ public:

```go
func findLucky(arr []int) int {
cnt := [510]int{}
cnt := [501]int{}
for _, x := range arr {
cnt[x]++
}
ans := -1
for x := 1; x < len(cnt); x++ {
if cnt[x] == x {
ans = x
for x := len(cnt) - 1; x > 0; x-- {
if x == cnt[x] {
return x
}
}
return ans
return -1
}
```

#### TypeScript

```ts
function findLucky(arr: number[]): number {
const cnt = Array(510).fill(0);
const cnt: number[] = Array(501).fill(0);
for (const x of arr) {
++cnt[x];
}
let ans = -1;
for (let x = 1; x < cnt.length; ++x) {
if (cnt[x] === x) {
ans = x;
for (let x = cnt.length - 1; x; --x) {
if (x === cnt[x]) {
return x;
}
}
return ans;
return -1;
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_lucky(arr: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
arr.iter().for_each(|&x| *cnt.entry(x).or_insert(0) += 1);
cnt.iter()
.filter(|(&x, &v)| x == v)
.map(|(&x, _)| x)
.max()
.unwrap_or(-1)
}
}
```

Expand All @@ -171,17 +180,16 @@ class Solution {
* @return Integer
*/
function findLucky($arr) {
$max = -1;
for ($i = 0; $i < count($arr); $i++) {
$hashtable[$arr[$i]] += 1;
$cnt = array_fill(0, 501, 0);
foreach ($arr as $x) {
$cnt[$x]++;
}
$keys = array_keys($hashtable);
for ($j = 0; $j < count($keys); $j++) {
if ($hashtable[$keys[$j]] == $keys[$j]) {
$max = max($max, $keys[$j]);
for ($x = 500; $x > 0; $x--) {
if ($cnt[$x] === $x) {
return $x;
}
}
return $max;
return -1;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class Solution {
public:
int findLucky(vector<int>& arr) {
int cnt[510];
memset(cnt, 0, sizeof(cnt));
int cnt[501]{};
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < 510; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = 500; x; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
func findLucky(arr []int) int {
cnt := [510]int{}
cnt := [501]int{}
for _, x := range arr {
cnt[x]++
}
ans := -1
for x := 1; x < len(cnt); x++ {
if cnt[x] == x {
ans = x
for x := len(cnt) - 1; x > 0; x-- {
if x == cnt[x] {
return x
}
}
return ans
return -1
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
class Solution {
public int findLucky(int[] arr) {
int[] cnt = new int[510];
for (int x : cnt) {
int[] cnt = new int[501];
for (int x : arr) {
++cnt[x];
}
int ans = -1;
for (int x = 1; x < cnt.length; ++x) {
if (cnt[x] == x) {
ans = x;
for (int x = cnt.length - 1; x > 0; --x) {
if (x == cnt[x]) {
return x;
}
}
return ans;
return -1;
}
}
Loading