티스토리 뷰
'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 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 안드로이드
- 소켓 프로그래밍
- defaultdict
- machine learning
- The Economist Espresso
- min heap
- 머신 러닝
- 리트코드
- Computer Graphics
- 티스토리챌린지
- tf-idf
- 파이썬
- join
- 이코노미스트 에스프레소
- Android
- C++
- ml
- socket programming
- Hash Map
- I2C
- DICTIONARY
- 투 포인터
- Python
- The Economist
- vertex shader
- 딕셔너리
- 오블완
- java
- 이코노미스트
- leetcode
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함
반응형