精華區beta Marginalman 關於我們 聯絡資訊
3005. Count Elements With Maximum Frequency 計算出現最多次的元素之次數之總和 Example 1: Input: nums = [1,2,2,3,1,4] Output: 4 Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array. So the number of elements in the array with maximum frequency is 4. Example 2: Input: nums = [1,2,3,4,5] Output: 5 Explanation: All elements of the array have a frequency of 1 which is the maximum. So the number of elements in the array with maximum frequency is 5. 思路: 用哈希表計算次數 最後比大小加總 Python Code: class Solution: def maxFrequencyElements(self, nums: List[int]) -> int: dic = {} for e in nums: if e in dic: dic[e] += 1 else: dic[e] = 1 m = max(dic.values()) return sum([v for v in dic.values() if v == m]) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.173.209 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709865194.A.5B1.html
JIWP: 大師,別卷了 03/08 10:34
sustainer123: 不捲就沒工作 對阿 03/08 10:34
yam276: 這種題目我都用哈希表 會不會有點懶人 03/08 10:36
DJYOSHITAKA: 大濕 03/08 10:38
sustainer123: 合理吧 直覺不就哈希表 03/08 10:43