saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 온라인저지
  • two pointer
  • 릿코드
  • 알고리즘문제해결
  • 개발자
  • Python
  • 취업준비
  • 딕셔너리
  • 취준
  • LeetCode
  • 개발자 취업준비
  • 리트코드
  • BFS
  • 문제해결능력
  • 파이썬
  • DFS
  • c++
  • 딥러닝
  • 알고리즘
  • 백준

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 1207. Unique Number of Occurrences

2022. 12. 1. 07:42

1207. Unique Number of Occurrences

Easy

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.

 

Example 1:

Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

Input: arr = [1,2]
Output: false

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true

 

Constraints:

  • 1 <= arr.length <= 1000
  • -1000 <= arr[i] <= 1000

문제 풀이

  • 숫자 배열이 주어지고, 숫자의 개수를 센다. 
  • 그 개수의 숫자가 중복되지 않을때 True를 리턴하고 중복이 된다면 False를 리턴한다.
  • 예를들어, [1, 1, 2, 2]가 있다면 1이 2개, 2가 2개이기 때문에 숫자 개수가 중복이되어 답은 False이다.
  • 반대로 [1, 2, 2]라면 1이 1개, 2가 2개이기 때문에 답은 True가 된다. 
  • 주어진 숫자를 각각 세는 Counter를 사용하여 숫자를 센다. O(N)
  • 그리고 set를 이용하여 Counter의 개수를 의미하는 value를 하나씩 set에 넣어서 중복되는지 확인한다. 

소스 코드

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        arr_cnt = Counter(arr)
        cnt_s = set()
        for k, v in arr_cnt.items():
            if v not in cnt_s:
                cnt_s.add(v)
            else:
                return False
        return True
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 1099. Two Sum Less Than K  (1) 2022.12.02
[LeetCode] 1704. Determine if String Halves Are Alike  (0) 2022.12.02
[LeetCode] 380. Insert Delete GetRandom O(1)  (0) 2022.11.30
[LeetCode] 253. Meeting Rooms II  (0) 2022.11.30
[LeetCode] 246. Strobogrammatic Number  (1) 2022.11.29
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 1099. Two Sum Less Than K
    • [LeetCode] 1704. Determine if String Halves Are Alike
    • [LeetCode] 380. Insert Delete GetRandom O(1)
    • [LeetCode] 253. Meeting Rooms II
    saurus2
    saurus2
    Simple is Best

    티스토리툴바