We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7379dc2 commit dd4e7f3Copy full SHA for dd4e7f3
solutions/1394-Find-Lucky-Integer-in-an-Array/1394.py
@@ -0,0 +1,17 @@
1
+from typing import List
2
+import collections
3
+
4
+class Solution:
5
+ def findLucky(self, arr: List[int]) -> int:
6
+ cnt = collections.Counter(arr)
7
+ # 先按频次排序,再按数值大小排序
8
+ res = sorted(cnt.items(), key=lambda x:(x[1],x[0]), reverse=True)
9
+ for i in range(len(res)):
10
+ # 频次等于数值大小
11
+ if res[i][0] == res[i][1]:
12
+ return res[i][0]
13
+ return -1
14
15
+if __name__ == "__main__":
16
+ arr = [2,2,3]
17
+ print(Solution().findLucky(arr))
0 commit comments