Re: [閒聊] 每日leetcode

看板Marginalman作者 (caster )時間1年前 (2024/03/08 10:33), 編輯推噓1(104)
留言5則, 4人參與, 1年前最新討論串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
文章代碼(AID): #1bwdZgMn (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bwdZgMn (Marginalman)