관리 메뉴

솜씨좋은장씨

[leetCode] 1207. Unique Number of Occurrences (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1207. Unique Number of Occurrences (Python)

솜씨좋은장씨 2020. 9. 23. 00:52
728x90
반응형

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

 

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

Solution

from collections import Counter

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        cnt = Counter(arr)
        
        cnt_list = [item[1] for item in cnt.items()]
        
        set_list = list(set(cnt_list))
        
        print(set_list)
        
        if len(cnt_list) != len(set_list):
            return False
        
        return True

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments