Code
Solution of problem 416 is showed below. You can also find it here.
1 | static int desyncio = []() { |
Solution
There are two solutions. The core method of both solutions is dp, specifically 0-1 Knapsack problem.
The first solution uses “bitset” to storage status.
The second solution, which doesn’t show here since it’s too simple and you need to implement it by yourself, uses “bool array” to storage all status.
The core operation (at line 16) of two solutions is different due to feature of the data structure they use. According to the result on LeetCode, the second solution is slower than the first one. In my opinion, bit operations is much faster than scanning through bool array.
Valueable Function
It is a good problem to practice “bitset” data structure. It is made of bits and can store more than 64 bits, which is the length of long long or unsigned long long. It also implements almost all bit operations.