2190. Most Frequent Number Following Key In an Array
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 59% Topics: Array, Hash Table, Counting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
import collections
# freq table
class Solution(object):
def mostFrequent(self, nums, key):
"""
:type nums: List[int]
:type key: int
:rtype: int
"""
return collections.Counter(nums[i+1] for i in xrange(len(nums)-1) if nums[i] == key).most_common(1)[0][0]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions