'169. Majority Element'에서 난이도가 조금 올라간 문제지만 큰 틀에선 똑같기에 솔루션 역시 흡사하다.
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Example 1:
Input: nums = [3,2,3]
Output: [3]
Example 2:
Input: nums = [1]
Output: [1]
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
dic = defaultdict(int)
for i, n in enumerate(nums):
dic[n] += 1
ret = []
for i, k in enumerate(dic):
if dic[k] > (len(nums) // 3):
ret.append(k)
return ret
참고
- https://leetcode.com/problems/majority-element-ii/description/
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
[LeetCode] 1151. Minimum Swaps to Group All 1's Together (0) | 2023.07.18 |
---|---|
[LeetCode] 392. Is Subsequence (0) | 2023.07.14 |
[LeetCode] 169. Majority Element (0) | 2023.07.12 |
[LeetCode] 70. Climbing Stairs (0) | 2023.07.11 |
[LeetCode] 1150. Check If a Number Is Majority Element in a Sorted Array (0) | 2023.07.10 |