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

'2025/05/12'에 해당되는 글 2건

Leetcode Problem:

Summary

  • Find all unique even integers that can be formed by concatenating three digits from the given array in any order, without leading zeros.

Approach

  • This solution uses a backtracking approach with recursion to generate all possible combinations of three digits from the input array
  • It checks if each generated number is even and inserts it into a set if it is
  • Finally, it returns a sorted vector of unique even numbers.

Complexity

  • O(2^n * n), where n is the number of digits in the input array
  • This is because in the worst case, the function generates all possible combinations of three digits, and for each combination, it checks if the generated number is even.

Explanation

  • The solution starts by sorting the input array to ensure that the digits are in ascending order
  • It then defines a recursive function combi to generate all possible combinations of three digits
  • For each combination, it checks if the generated number is even and inserts it into a set if it is
  • Finally, it returns a sorted vector of unique even numbers.

Solution Code:


class Solution {
public:
    set s;
    vector ans;
    bool visited[100] = {false,};
    void combi(vector& digits, int depth, int num){
        if(depth == 3){
            if(num % 2 == 0){
                s.insert(num);
            }
            return;
        }
        for(int i = 0 ; i < digits.size(); i++){
            if(visited[i]) continue;
            int n = num * 10 + digits[i];
            if(n == 0) continue;
            visited[i] = true;
            combi(digits, depth+1, n);
            visited[i] = false;
        }
    }
    vector findEvenNumbers(vector& digits) {
        sort(digits.begin(), digits.end());
        combi(digits, 0, 0);
        for (int num : s){
            ans.push_back(num);
        }
        return ans;
    }
};
블로그 이미지

짬뽕얼큰하게

,

Leetcode Problem:

Summary

  • Given two arrays of positive integers, replace all 0's with positive integers to make the sum of both arrays equal, and return the minimum equal sum.

Approach

  • This solution first calculates the sum of each array and the number of zeros in each array
  • It then checks if it's possible to make the sum of both arrays equal by replacing all zeros with positive integers
  • If it's possible, it returns the maximum sum of the two arrays, which is the minimum equal sum
  • If it's not possible, it returns -1.

Complexity

  • O(n), where n is the total number of elements in both arrays.

Explanation

  • The solution iterates over each array to calculate the sum and count of zeros
  • It then checks if replacing all zeros with positive integers is possible by checking if the sum of one array is greater than the sum of the other array
  • If it's possible, it returns the maximum sum of the two arrays
  • If it's not possible, it returns -1.

Solution Code:


class Solution {
public:
    long long minSum(vector& nums1, vector& nums2) {
        long long sum1 = 0, sum2 = 0;
        long long zero1 = 0, zero2 = 0;
        for (int i : nums1) {
            sum1 += i;
            if (i == 0) {
                sum1++;
                zero1++;
            }
        }
        for (int i : nums2) {
            sum2 += i;
            if (i == 0) {
                sum2++;
                zero2++;
            }
        }
        if (!zero1 && sum2 > sum1 || !zero2 && sum1 > sum2) {
            return -1;
        }
        return max(sum1, sum2);
    }
};

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

Total Characters in String After Transformations I  (0) 2025.05.14
Finding 3-Digit Even Numbers  (1) 2025.05.12
Three Consecutive Odds  (0) 2025.05.11
Count Number of Balanced Permutations  (1) 2025.05.10
Find Minimum Time to Reach Last Room II  (1) 2025.05.08
블로그 이미지

짬뽕얼큰하게

,