목차


Bitmask를 이용한 모든 부분집합 구하기

class Solution {
public:
    int maxProduct(string s) {
        unique_ptr<vector<int>> palindromic_masks = get_palindromic_masks(s);
        int max_product = max_product_of_two_palindromic_subsequences(*palindromic_masks);

        return max_product;
    }

private:
    unique_ptr<vector<int>> get_palindromic_masks(const string& s) {
        int n = s.size();
        unique_ptr<vector<int>> palindromic_masks = make_unique<vector<int>>();
        for (int mask = 1; mask < (1 << n); ++mask) {
            string subseq = "";
            for (int i = 0; i < n; ++i) {
                if (mask & (1 << i)) {
                    subseq += s[i];
                }
            }

            if (is_palindrome(subseq)) {
                palindromic_masks->push_back(mask);
            }
        }

        return palindromic_masks;
    }

    int max_product_of_two_palindromic_subsequences(const vector<int>& palindromic_masks) {
        int max_product = 0;
        for (int i = 0; i < palindromic_masks.size(); ++i) {
            for (int j = i + 1; j < palindromic_masks.size(); ++j) {
                if ((palindromic_masks[i] & palindromic_masks[j]) == 0) {
                    int product = bit_count(palindromic_masks[i]) * bit_count(palindromic_masks[j]);
                    max_product = max(max_product, product);
                }
            }
        }

        return max_product;
    }

    bool is_palindrome(const string& s) {
        int l = 0, r = s.size() - 1;
        while (l < r) {
            if (s[l++] != s[r--]) return false;
        }
        return true;
    }

    int bit_count(unsigned int x) {
        int count = 0;
        while (x) {
            count += x & 1;
            x >>= 1;
        }
        return count;
    }
};

DP를 이용한 최적화 문제

class Solution {
public:
    int maximizeTheProfit(int n, vector<vector<int>>& offers) {
        vector<int> dp(n + 1, 0); // dp[i]는 길이가 i인 길의 최대 이익
        vector<vector<pair<int, int>>> offersEndingAt(n); // offersEndingAt[i]는 길의 끝이 i인 제안들(시작, 금액)
        // Group offers by their end positions
        for (const auto& offer : offers) {
            int start = offer[0];
            int end = offer[1];
            int gold = offer[2];
            offersEndingAt[end].emplace_back(start, gold);
        }
        // Dynamic Programming to calculate maximum profit
        for (int i = 0; i < n; ++i) {
            dp[i + 1] = dp[i]; // Initialize dp[i+1] with dp[i]
            for (const auto& [start, gold] : offersEndingAt[i]) {
                dp[i + 1] = max(dp[i + 1], dp[start] + gold); // dp[i+1]은 start에서 끝나는 제안의 금액 + dp[start] 중 최대값
            }
        }
        return dp[n];
    }
};

hashtable을 통해 array를 생성하기

class Solution {
public:
    bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
        map<int, vector<int>> mp;
        for (auto& p : pieces) {
            mp[p[0]] = p;
        }
        vector<int> res;
        for (int i = 0; i < arr.size(); i++) {
            if (mp.find(arr[i]) != mp.end()) {
                res.insert(res.end(), mp[arr[i]].begin(), mp[arr[i]].end());
            }
        }
        return res == arr;
    }
};

2024-10-28
카테고리로 돌아가기 ↩