Leetcode 1207. Unique Number of Occurrences

Problem

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

Algorithm

Sort the array and test the adjoint numbers.

Code

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        cnts = [0] * 1001
        for a in arr:
            cnts[a] += 1
        cnts.sort(reverse=True)
        
        index = 1
        while index < 1001 and cnts[index]:
            if cnts[index] == cnts[index-1]:
                return False
            index += 1
        return True

你可能感兴趣的:(Leetcode,入门题,leetcode,算法,职场和发展)