Skip to content

Commit 14379c3

Browse files
committed
feat: add solutions to lc problem: No.1394
No.1394.Find Lucky Integer in an Array
1 parent 3dcc2cd commit 14379c3

File tree

6 files changed

+91
-76
lines changed

6 files changed

+91
-76
lines changed

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

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:计数**
67+
68+
我们可以用哈希表或数组 $cnt$ 统计 $arr$ 中每个数字出现的次数,然后遍历 $cnt$,找到满足 $cnt[x] = x$ 的最大的 $x$ 即可。如果没有这样的 $x$,则返回 $-1$。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $arr$ 的长度。
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**
@@ -72,11 +78,11 @@
7278
```python
7379
class Solution:
7480
def findLucky(self, arr: List[int]) -> int:
75-
counter = Counter(arr)
81+
cnt = Counter(arr)
7682
ans = -1
77-
for num, n in counter.items():
78-
if num == n and ans < num:
79-
ans = num
83+
for x, v in cnt.items():
84+
if x == v and ans < x:
85+
ans = x
8086
return ans
8187
```
8288

@@ -87,14 +93,14 @@ class Solution:
8793
```java
8894
class Solution {
8995
public int findLucky(int[] arr) {
90-
Map<Integer, Integer> mp = new HashMap<>();
91-
for (int num : arr) {
92-
mp.put(num, mp.getOrDefault(num, 0) + 1);
96+
int[] cnt = new int[510];
97+
for (int x : cnt) {
98+
++cnt[x];
9399
}
94100
int ans = -1;
95-
for (int num : arr) {
96-
if (num == mp.get(num) && ans < num) {
97-
ans = num;
101+
for (int x = 1; x < cnt.length; ++x) {
102+
if (cnt[x] == x) {
103+
ans = x;
98104
}
99105
}
100106
return ans;
@@ -108,12 +114,16 @@ class Solution {
108114
class Solution {
109115
public:
110116
int findLucky(vector<int>& arr) {
111-
int n = 510;
112-
vector<int> counter(n);
113-
for (int e : arr) ++counter[e];
117+
int cnt[510];
118+
memset(cnt, 0, sizeof(cnt));
119+
for (int x : arr) {
120+
++cnt[x];
121+
}
114122
int ans = -1;
115-
for (int i = 1; i < n; ++i) {
116-
if (i == counter[i] && ans < i) ans = i;
123+
for (int x = 1; x < 510; ++x) {
124+
if (cnt[x] == x) {
125+
ans = x;
126+
}
117127
}
118128
return ans;
119129
}
@@ -124,18 +134,17 @@ public:
124134
125135
```go
126136
func findLucky(arr []int) int {
127-
n := 510
128-
counter := make([]int, n)
129-
for _, e := range arr {
130-
counter[e]++
131-
}
132-
ans := -1
133-
for i := 1; i < n; i++ {
134-
if i == counter[i] && ans < i {
135-
ans = i
136-
}
137-
}
138-
return ans
137+
cnt := [510]int{}
138+
for _, x := range arr {
139+
cnt[x]++
140+
}
141+
ans := -1
142+
for x := 1; x < len(cnt); x++ {
143+
if cnt[x] == x {
144+
ans = x
145+
}
146+
}
147+
return ans
139148
}
140149
```
141150

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
```python
5151
class Solution:
5252
def findLucky(self, arr: List[int]) -> int:
53-
counter = Counter(arr)
53+
cnt = Counter(arr)
5454
ans = -1
55-
for num, n in counter.items():
56-
if num == n and ans < num:
57-
ans = num
55+
for x, v in cnt.items():
56+
if x == v and ans < x:
57+
ans = x
5858
return ans
5959
```
6060

@@ -63,14 +63,14 @@ class Solution:
6363
```java
6464
class Solution {
6565
public int findLucky(int[] arr) {
66-
Map<Integer, Integer> mp = new HashMap<>();
67-
for (int num : arr) {
68-
mp.put(num, mp.getOrDefault(num, 0) + 1);
66+
int[] cnt = new int[510];
67+
for (int x : cnt) {
68+
++cnt[x];
6969
}
7070
int ans = -1;
71-
for (int num : arr) {
72-
if (num == mp.get(num) && ans < num) {
73-
ans = num;
71+
for (int x = 1; x < cnt.length; ++x) {
72+
if (cnt[x] == x) {
73+
ans = x;
7474
}
7575
}
7676
return ans;
@@ -84,12 +84,16 @@ class Solution {
8484
class Solution {
8585
public:
8686
int findLucky(vector<int>& arr) {
87-
int n = 510;
88-
vector<int> counter(n);
89-
for (int e : arr) ++counter[e];
87+
int cnt[510];
88+
memset(cnt, 0, sizeof(cnt));
89+
for (int x : arr) {
90+
++cnt[x];
91+
}
9092
int ans = -1;
91-
for (int i = 1; i < n; ++i) {
92-
if (i == counter[i] && ans < i) ans = i;
93+
for (int x = 1; x < 510; ++x) {
94+
if (cnt[x] == x) {
95+
ans = x;
96+
}
9397
}
9498
return ans;
9599
}
@@ -100,18 +104,17 @@ public:
100104
101105
```go
102106
func findLucky(arr []int) int {
103-
n := 510
104-
counter := make([]int, n)
105-
for _, e := range arr {
106-
counter[e]++
107-
}
108-
ans := -1
109-
for i := 1; i < n; i++ {
110-
if i == counter[i] && ans < i {
111-
ans = i
112-
}
113-
}
114-
return ans
107+
cnt := [510]int{}
108+
for _, x := range arr {
109+
cnt[x]++
110+
}
111+
ans := -1
112+
for x := 1; x < len(cnt); x++ {
113+
if cnt[x] == x {
114+
ans = x
115+
}
116+
}
117+
return ans
115118
}
116119
```
117120

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
class Solution {
22
public:
33
int findLucky(vector<int>& arr) {
4-
int n = 510;
5-
vector<int> counter(n);
6-
for (int e : arr) ++counter[e];
4+
int cnt[510];
5+
memset(cnt, 0, sizeof(cnt));
6+
for (int x : arr) {
7+
++cnt[x];
8+
}
79
int ans = -1;
8-
for (int i = 1; i < n; ++i) {
9-
if (i == counter[i] && ans < i) ans = i;
10+
for (int x = 1; x < 510; ++x) {
11+
if (cnt[x] == x) {
12+
ans = x;
13+
}
1014
}
1115
return ans;
1216
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
func findLucky(arr []int) int {
2-
n := 510
3-
counter := make([]int, n)
4-
for _, e := range arr {
5-
counter[e]++
2+
cnt := [510]int{}
3+
for _, x := range arr {
4+
cnt[x]++
65
}
76
ans := -1
8-
for i := 1; i < n; i++ {
9-
if i == counter[i] && ans < i {
10-
ans = i
7+
for x := 1; x < len(cnt); x++ {
8+
if cnt[x] == x {
9+
ans = x
1110
}
1211
}
1312
return ans

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int findLucky(int[] arr) {
3-
Map<Integer, Integer> mp = new HashMap<>();
4-
for (int num : arr) {
5-
mp.put(num, mp.getOrDefault(num, 0) + 1);
3+
int[] cnt = new int[510];
4+
for (int x : cnt) {
5+
++cnt[x];
66
}
77
int ans = -1;
8-
for (int num : arr) {
9-
if (num == mp.get(num) && ans < num) {
10-
ans = num;
8+
for (int x = 1; x < cnt.length; ++x) {
9+
if (cnt[x] == x) {
10+
ans = x;
1111
}
1212
}
1313
return ans;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def findLucky(self, arr: List[int]) -> int:
3-
counter = Counter(arr)
3+
cnt = Counter(arr)
44
ans = -1
5-
for num, n in counter.items():
6-
if num == n and ans < num:
7-
ans = num
5+
for x, v in cnt.items():
6+
if x == v and ans < x:
7+
ans = x
88
return ans

0 commit comments

Comments
 (0)