python C C++陣列迴圈速解Leetcode 3005 Count Elements With Maximum Frequency
----
Array loops can solve the problem. Why use hash table (C++ unordered_map)? Simple question can be answered with simple data structures. Loops are loops and arrays are arrays.
[codes on Leetcode]https://leetcode.com/problems/count-elements-with-maximum-frequency/solutions/4840098/c-c-python-freq-array-counting-0ms-beats-100/
[Leetcode playList]https://www.youtube.com/watch?v=WjrWkPysfRM&list=PLYRlUBnWnd5IdDHk2BjqXwesydU17z_xk
[Dynamic Programming Playlist]https://www.youtube.com/watch?v=30yq3fmE6E8&list=PLYRlUBnWnd5K_XYUesV9oc6M9ONXII61T
- class Solution:
- def maxFrequencyElements(self, nums: List[int]) -> int:
- freq=[0]*101
- maxF=0
- for x in nums:
- freq[x]+=1
- maxF=max(maxF, freq[x])
- ans=0
- for f in freq:
- if f==maxF:
- ans+=f
- return ans
沒有留言:
張貼留言