2044 - Counting the number of subsets that can get the maximum value by bitwise OR

Directory link: force buckle programming problem - solution summary_share + record - CSDN blog

Original title link: force buckle


describe:

Given an array nums of integers, please find the maximum possible bitwise OR of subsets of nums, and return the number of distinct non-empty subsets whose bitwise OR can obtain the maximum.

Array a is considered to be a subset of array b if it can be obtained by removing some elements (or not) from array b. If the subscript positions of the selected elements are different, the two subsets are considered to be different.

Performs a bitwise OR on the array a, the result is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-based index).

Example 1:

Input: nums = [3,1]
Output: 2
Explanation: The maximum value that can be obtained by bitwise OR of subsets is 3. Bitwise OR of 2 subsets can get 3:
- [3]
- [3,1]
Example 2:

Input: nums = [2,2,2]
Output: 7
Explanation: The bitwise OR of all non-empty subsets of [2,2,2] gives 2. There are 23 - 1 = 7 subsets in total.
Example 3:

Input: nums = [3,2,1,5]
Output: 6
Explanation: Bitwise OR of subsets The maximum possible value is 7. Bitwise OR of 6 subsets can get 7:
- [3,5]
- [3,1,5]
- [3,2,5]
- [3,2,1,5]
- [2,5]
- [2,1,5]
 

hint:

1 <= nums.length <= 16
1 <= nums[i] <= 105

Source: LeetCode
Link: https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets The
copyright belongs to Leetcode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Problem solving ideas:

* Problem-solving ideas: 
* Divided into two steps, the first step is to find the largest mask (the possible maximum value of subset bitwise OR is 7). The second step is to find the number of matches 
* The first step is to find the largest mask, we can use the OR operation, and each number is ORed with mask1, then the final mask1 is the maximum possible value of the bitwise OR of the subset. 
* The second step is because each number has only two possibilities: choice and no choice. Then we can use the recursive method, first use no selection in each recursion, and then use selection. When using select, you need to modify the current or result: currentValue

Code:

public class Solution2044 {
    int sum = 0;

    public int countMaxOrSubsets(int[] nums) {
        //最大值mask
        int mask1 = 0;
        for (int i = 0; i < nums.length; i++) {
            int value = nums[i];
            mask1 |= value;
        }
        search(nums, 0, 0, mask1);
        return sum;
    }

    private void search(int[] nums, int index, int currentValue, int mask) {
        if (index >= nums.length) {
            if (currentValue == mask) {
                sum++;
            }
            return;
        }
        int num = nums[index];
        //不选
        search(nums, ++index, currentValue, mask);

        currentValue = currentValue | num;
        //选择
        search(nums, index, currentValue, mask);
    }
}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/123499085