Pure Bit Manipulation


In this post, I will introduce problems related to bit manipulation. Before that, you may want to learn the std::bitset<> class template.


LC 1386. Cinema Seat Allocation

The problem is not difficult. But it is tricky to write a clean code. The hint is that all valid patterns can be presented by the predefined bit-sets.

auto const p1 = bitset<10>(0b011'1100'000);
auto const p2 = bitset<10>(0b000'1111'000);
auto const p3 = bitset<10>(0b000'0011'110);

LC 1452. People Whose List of Favorite Companies Is Not a Subset of Another List

Bitsets support fast intersect and union of unordered sets.

// intersect
a & b
// union
a | b
// check if a is in b
(a & b) == a

Leave a Reply

Your email address will not be published. Required fields are marked *