DP & Bitmask
DP & Bitmask
DP & Bitmask
2
1. Why using DP with
Bitmask?
3
[EngineerPro] DP & Bitmask
4
2. Examples
5
[EngineerPro] DP & Bitmask
- Observation 3: Given a state (visited, last), we can try every node to move next.
- new_visited = visited | (1 << next)
- cost(last, next) = ? => Floyd-Warshall algorithm
6
[EngineerPro] DP & Bitmask
- Observation 2: Let dp[mask] be the min no. people to get skills mask, we can try to add more people to
improve the mask: new_mask = mask | people[i]. The result is f[(1 << len(skills)) - 1].
7
[EngineerPro] DP & Bitmask
- Observation 2: From the bitmask, we know how many numbers are used, hence, how many operations
have been done: current turn = mask.bit_count() / 2 + 1
- Observation 3: Let dp[mask] be the max score after the set of numbers in mask is used.
After using two numbers nums[u] and nums[v], the new mask is:
8
[EngineerPro] DP & Bitmask
9
3. Homework
10
[EngineerPro] DP & Bitmask
11