herrDeng網內搜尋

自訂搜尋

Ads

2024年3月8日 星期五

python C C++陣列迴圈速解Leetcode 3005 Count Elements With Maximum Frequency


python C C++陣列迴圈速解Leetcode 3005  Count Elements With Maximum Frequency
陣列迴圈就能解答的,何須用到hash table(C++ unordered_map)簡單的問題可用簡單的 資料結構解答,迴圈就是迴圈、陣列就是陣列(內有python code)
----
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.


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

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章