짬뽕얼큰하게의 맨땅에 헤딩 :: '2025/03/17 글 목록

'2025/03/17'에 해당되는 글 1건

Leetcode Problem:

Summary

  • Check if an array of integers can be divided into pairs of equal numbers.

Approach

  • Count the occurrences of each number in the array and check if all counts are even
  • If any count is odd, return false
  • Otherwise, return true.

Complexity

  • O(n) where n is the length of the input array

Explanation

  • The solution first counts the occurrences of each number in the array using an array cnt
  • Then, it checks if all counts are even by iterating from 1 to 500
  • If any count is odd, it returns false
  • Otherwise, it returns true.

Solution Code:


class Solution {
public:
    int cnt[501] = {0};
    bool divideArray(vector& nums) {
        for(int n : nums){
            cnt[n]++;
        }
        for(int i = 1; i <= 500; i++){
            if(cnt[i] &1){
                return false;
            }
        }
        return true;
    }
};

'알고리즘' 카테고리의 다른 글

Split Linked List in Parts  (0) 2025.03.19
Longest Nice Subarray  (0) 2025.03.18
Minimum Time to Repair Cars  (0) 2025.03.16
House Robber IV  (0) 2025.03.15
Zero Array Transformation II  (0) 2025.03.14
블로그 이미지

짬뽕얼큰하게

,