LeetCode //C - 1207. Unique Number of Occurrences

1207. Unique Number of Occurrences

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

From: LeetCode
Link: 1207. Unique Number of Occurrences


Solution:

Ideas:

We can use a hash table (or an array, considering the constraints) to count occurrences. Then, we use another hash table to check for the uniqueness of these counts.

  • occurrences is an array indexed from -1000 to 1000 (shifted by 1000 to accommodate negative numbers), used to count the occurrences of each integer in arr.
  • seen is a boolean array used to track if a specific occurrence count has been seen before.
  • We first iterate through the arr to count the occurrences of each number.
  • Then, we iterate through the occurrences array to check if any count has been seen before. If a count is repeated, we return false. If we finish the loop without finding any duplicate counts, we return true.
Code:
bool uniqueOccurrences(int* arr, int arrSize) {
   
    int occurrences[2001] = {
   0}; // Array to count occurrences, offset by 1000 for negative numbers
    bool seen[1001] = {
   false}; // Array to track if an occurrence count has been seen

    // Count occurrences of each number
    for (int i = 0; i < arrSize; i++) {
   
        occurrences[arr[i] + 1000]++;
    }

    // Check for unique occurrences
    for (int i = 0; i < 2001; i++) {
   
        if (occurrences[i] > 0) {
   
            if (seen[occurrences[i]]) {
   
                // If we have already seen this count, it's not unique
                return false;
            }
            seen[occurrences[i]] = true;
        }
    }

    return true;
}

相关推荐

  1. 1207. 大臣的旅费

    2023-12-31 09:02:04       14 阅读
  2. codeforces 1200E

    2023-12-31 09:02:04       36 阅读
  3. 题目 1209: 密码截获

    2023-12-31 09:02:04       31 阅读
  4. AcWing 1227. 分巧克力

    2023-12-31 09:02:04       17 阅读
  5. LeetCode 1378、1277、2944

    2023-12-31 09:02:04       24 阅读
  6. LeetCode //C - 1207. Unique Number of Occurrences

    2023-12-31 09:02:04       39 阅读
  7. PYTHON 120道题目详解(118-120

    2023-12-31 09:02:04       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-31 09:02:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-31 09:02:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-31 09:02:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-31 09:02:04       20 阅读

热门阅读

  1. pytorch 多卡训练 accelerate gloo

    2023-12-31 09:02:04       38 阅读
  2. FPGA和DSP的区别 FPGA与DSP的区别(粗略整理)

    2023-12-31 09:02:04       45 阅读
  3. dbf文件批量合成excel

    2023-12-31 09:02:04       39 阅读
  4. minio命令行详解

    2023-12-31 09:02:04       36 阅读
  5. mysql聚合函数andjson_object 例子

    2023-12-31 09:02:04       32 阅读
  6. 【全网首发】洛谷P2678 [NOIP2015 提高组] 跳石头

    2023-12-31 09:02:04       39 阅读
  7. redis在linux集群部署

    2023-12-31 09:02:04       28 阅读