1. 문제 이해
정수 리스트 arr가 주어진다.
각 숫자가 몇 번씩 등장하는지 확인하고, 각 숫자의 등장 횟수가 모두 고유한지를 판단한다.
2. 풀이 과정
Python의 딕셔너리 자료형을 사용한다.
defaultdict을 사용한다. defaultdict에서는 새 키가 추가될 때, 기본값 0이 자동으로 설정된다.
각 숫자들의 등장 횟수는 딕셔너리의 value가 된다.
value들의 개수와 set으로 중복을 제거한 value들의 개수가 같은지 비교한다.
만약 같다면 등장 횟수가 고유한 것이다.
3. 전체 코드
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
d = defaultdict(int)
for num in arr:
d[num] += 1
return len(d.values()) == len(set(d.values()))
4. 생각할 점
- Python에서 len 함수는 set뿐만 아니라 어떤 이터러블 자료형에도 사용할 수 있다.
5. 출처
- https://leetcode.com/problems/unique-number-of-occurrences/?envType=study-plan-v2&envId=leetcode-75
'Algorithms > LeetCode' 카테고리의 다른 글
[LeetCode] 2352. Equal Row and Column Pairs (1) | 2024.11.15 |
---|---|
[LeetCode] 1657. Determine if Two Strings Are Close (0) | 2024.11.14 |
[LeetCode] 2215. Find the Difference of Two Arrays (0) | 2024.11.12 |
[LeetCode] 724. Find Pivot Index (0) | 2024.11.11 |
[LeetCode] 1732. Find the Highest Altitude (0) | 2024.11.10 |