Last updated
Was this helpful?
Last updated
Was this helpful?
Given an array of integers arr
, a lucky integer is an integer which has a frequency in the array equal to its value.
Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
1 <= arr.length <= 500
1 <= arr[i] <= 500
这题一看,不久是用map来数数吗?后来也想了一下sort的解法。sort了之后,从屁股开始找,第一个等于下标的就是max了。T:O(nlogn), S:O(1)。用hashmap的话,就是先过一遍数频率,再过一遍找max。T:O(n), S:O(n)。