1. 程式人生 > 其它 >2212. 射箭比賽中的最大得分 狀態壓縮

2212. 射箭比賽中的最大得分 狀態壓縮

狀態壓縮

class Solution {
public:
    vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
        int msk = 0;
        int maxScore = 0;

        // i: 遍歷2^12種結果
        for (int i = 1; i < (1 << 12); i++) {
            int count = 0; // 弓箭數
            int score = 0; // 得分
            for (int j = 0; j < 12; j++) {
                if (i & (1 << j)) {
                    count += aliceArrows[j] + 1;
                    score += j;
                }
            }
            if (count <= numArrows && score > maxScore) {
                msk = i;
                maxScore = score;
            }
        }

        vector <int> ans(12);
        for (int j = 0; j < 12; j++) {
            if (msk & (1 << j)) {
                ans[j] = aliceArrows[j] + 1;
                numArrows -= aliceArrows[j] + 1;
            }
        }
        ans[0] += numArrows;

        return ans;
    }
};