Re: [閒聊] 每日leetcode
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/03/08 10:33)推噓1(1推 0噓 4→)留言5則, 4人參與討論串25/1548 (看更多)
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
→
03/08 10:34,
1年前
, 1F
03/08 10:34, 1F
→
03/08 10:34,
1年前
, 2F
03/08 10:34, 2F
→
03/08 10:36,
1年前
, 3F
03/08 10:36, 3F
推
03/08 10:38,
1年前
, 4F
03/08 10:38, 4F
→
03/08 10:43,
1年前
, 5F
03/08 10:43, 5F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 25 之 1548 篇):