From 918662ef7f5c039ef89e6a10c48d43affae77b0f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 5 Jul 2025 06:38:25 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1394 No.1394.Find Lucky Integer in an Array --- .../README.md | 90 ++++++++++--------- .../README_EN.md | 90 ++++++++++--------- .../Solution.cpp | 12 ++- .../Solution.go | 11 ++- .../Solution.java | 13 ++- .../Solution.php | 17 ++-- .../Solution.py | 6 +- .../Solution.rs | 13 +++ .../Solution.ts | 11 ++- 9 files changed, 141 insertions(+), 122 deletions(-) create mode 100644 solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.rs diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md index ad8e3171ec677..e2aab03d4d2ff 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md @@ -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}$ 的长度。 @@ -93,11 +93,7 @@ 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 @@ -105,17 +101,16 @@ class Solution: ```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; } } ``` @@ -126,18 +121,16 @@ class Solution { class Solution { public: int findLucky(vector& 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; } }; ``` @@ -146,17 +139,16 @@ 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 } ``` @@ -164,17 +156,34 @@ func findLucky(arr []int) int { ```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 { + 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) + } } ``` @@ -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; } } ``` diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md index 0150e92bf8adf..cf0c8ede706fc 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md @@ -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}$. @@ -77,11 +77,7 @@ 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 @@ -89,17 +85,16 @@ class Solution: ```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; } } ``` @@ -110,18 +105,16 @@ class Solution { class Solution { public: int findLucky(vector& 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; } }; ``` @@ -130,17 +123,16 @@ 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 } ``` @@ -148,17 +140,34 @@ func findLucky(arr []int) int { ```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 { + 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) + } } ``` @@ -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; } } ``` diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp index fc249af1dfb56..bd184b1b614af 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp @@ -1,17 +1,15 @@ class Solution { public: int findLucky(vector& 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; } }; \ No newline at end of file diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.go b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.go index c7cc0cf11c3d8..2065349fea7da 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.go +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.go @@ -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 } \ No newline at end of file diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java index b46b186230051..e032656eb678a 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java @@ -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; } } \ No newline at end of file diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.php b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.php index 978bf0079a7ce..d75690dce4b15 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.php +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.php @@ -4,16 +4,15 @@ 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; } -} +} \ No newline at end of file diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py index 1c1d2594cc4e6..d374650ec8dab 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py @@ -1,8 +1,4 @@ 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) diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.rs b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.rs new file mode 100644 index 0000000000000..89dbef71a7a4d --- /dev/null +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +impl Solution { + pub fn find_lucky(arr: Vec) -> 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) + } +} diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.ts b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.ts index 2effea7d2bc38..719deeeba14a6 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.ts +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.ts @@ -1,13 +1,12 @@ 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; }