Skip to content

Commit 8d85100

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.1 MB (11.24%)
1 parent 84e3d37 commit 8d85100

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Given an array of integers <code>arr</code>, a <strong>lucky integer</strong> is an integer that has a frequency in the array equal to its value.</p>
2+
3+
<p>Return <em>the largest <strong>lucky integer</strong> in the array</em>. If there is no <strong>lucky integer</strong> return <code>-1</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> arr = [2,2,3,4]
10+
<strong>Output:</strong> 2
11+
<strong>Explanation:</strong> The only lucky number in the array is 2 because frequency[2] == 2.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> arr = [1,2,2,3,3,3]
18+
<strong>Output:</strong> 3
19+
<strong>Explanation:</strong> 1, 2 and 3 are all lucky numbers, return the largest of them.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> arr = [2,2,2,3,3]
26+
<strong>Output:</strong> -1
27+
<strong>Explanation:</strong> There are no lucky numbers in the array.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= arr.length &lt;= 500</code></li>
35+
<li><code>1 &lt;= arr[i] &lt;= 500</code></li>
36+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Approach 3: Counter
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def findLucky(self, arr: List[int]) -> int:
10+
counts = Counter(arr)
11+
lucky_num = -1
12+
13+
for num, count in counts.items():
14+
if num == count:
15+
lucky_num = max(lucky_num, num)
16+
17+
return lucky_num
18+

0 commit comments

Comments
 (0)