my submission is https://leetcode.com/problems/top-k-frequent-elements/submissions/912663494/
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
'''counting keywords and their frequencies into a dict'''
record = {}
for num in nums:
if num not in record:
record[num] = 1
else:
record[num] += 1
'''sort the dict according to the frequencies with reverse order'''
topk = list(dict(sorted(record.items(), key=lambda items: items[1], reverse=True)))[:k]
return topk
if __name__ == "__main__":
sol = Solution()
nums = [4,1,-1,2,-1,2,3]
output = sol.topKFrequent(nums=nums, k=2)
this is what I got when running the code above in pycharm,
however, when I submit my answer on leecode, it is judged as a wrong answer, as below:
Could someone please tell where the problem is?