[leetcode] Minimum Number of K Consecutive Bit Flips

我永远都学不会贪心算法罢了(╯‵□′)╯︵┻━┻

 

class Solution {
public:
    int minKBitFlips(vector<int>& A, int K) {
        int count=0, A_length=A.size();
        queue<int> flip_records;
        bool has_flipped, need_flip;
        for (int i=0;i<A_length;i++) {
            if (!flip_records.empty()) {
                if (flip_records.front()+K-1<i)
                    flip_records.pop();
            }
            has_flipped=flip_records.size()%2?true:false;
            need_flip=!A[i]^has_flipped;
            if (need_flip) {
                if (i+K>A_length)
                    return -1;
                flip_records.push(i);
                ++count;
            }
        }
        return count;
    }
};

 

上一篇:Kafka Producer Notes


下一篇:PAT A1028 List Sorting 列表排序【简单排序 】