Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-03-08 10:33:10
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])
作者: JIWP (JIWP)   2024-03-08 10:34:00
大師,別卷了
作者: sustainer123 (caster)   2024-03-08 10:34:00
不捲就沒工作 對阿
作者: yam276 ('_')   2024-03-08 10:36:00
這種題目我都用哈希表 會不會有點懶人
作者: DJYOSHITAKA (Evans)   2024-03-08 10:38:00
大濕
作者: sustainer123 (caster)   2024-03-08 10:43:00
合理吧 直覺不就哈希表

Links booklink

Contact Us: admin [ a t ] ucptt.com