diff --git a/LeetCode/top_k_frequent.py b/LeetCode/top_k_frequent.py new file mode 100644 index 0000000..ed48641 --- /dev/null +++ b/LeetCode/top_k_frequent.py @@ -0,0 +1,49 @@ +''' +Given a non-empty array of integers, return the k most frequent elements. + +Example 1: + +Input: nums = [1,1,1,2,2,3], k = 2 +Output: [1,2] +Example 2: + +Input: nums = [1], k = 1 +Output: [1] + +''' + +class Solution: + + def topKFrequent(self, nums, k): + + """ + :type nums: List[int] + :type k: int + :rtype: List[int] + """ + + m,v,ma={},[],0 + + for i in nums: + + if i in m: + m[i]+=1 + + else: + m[i]=1 + + if m[i]>ma: + ma=m[i] + + while k!=0: + + for i in m: + + if k==0: + break + + if m[i]==ma: + v.append(i) + k-=1 + ma-=1 + return v \ No newline at end of file diff --git a/test/test_top_frequent.py b/test/test_top_frequent.py new file mode 100644 index 0000000..2920dc9 --- /dev/null +++ b/test/test_top_frequent.py @@ -0,0 +1,8 @@ +import sys +import os +sys.path.append(os.path.join(sys.path[0], '..')) +from LeetCode.top_k_frequent import Solution + +def test_topk(): + s = Solution() + assert s.topKFrequent([1,1,1,2,2,3],2) == [1,2] \ No newline at end of file