Skip to content

Commit

Permalink
DP top-down solution to "Count Number of Maximum Bitwise-OR Subsets"
Browse files Browse the repository at this point in the history
problem
  • Loading branch information
dkoval committed Oct 18, 2024
1 parent 92c01da commit b230c7c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.dkoval.leetcode.challenge;

/**
* <a href="https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/">Count Number of Maximum Bitwise-OR Subsets</a>
* <p>
* Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different
* non-empty subsets with the maximum bitwise OR.
* <p>
* An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.
* Two subsets are considered different if the indices of the elements chosen are different.
* <p>
* The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
* <p>
* Constraints:
* <ul>
* <li>1 <= nums.length <= 16</li>
* <li>1 <= nums[i] <= 10^5</li>
* </ul>
*/
public interface CountNumberOfMaximumBitwiseORSubsets {

int countMaxOrSubsets(int[] nums);

class CountNumberOfMaximumBitwiseORSubsetsRev1 implements CountNumberOfMaximumBitwiseORSubsets {

@Override
public int countMaxOrSubsets(int[] nums) {
// max bitwise-or
int target = 0;
for (int x : nums) {
target |= x;
}
// DP top-down; caching is not needed due to light constraints
return calc(nums, 0, 0, target);
}

private int calc(int[] nums, int index, int current, int target) {
if (index == nums.length) {
return (current == target) ? 1 : 0;
}

int count = 0;
// option #1: take nums[index]
count += calc(nums, index + 1, current | nums[index], target);
// option #2: skip nums[index]
count += calc(nums, index + 1, current, target);
return count;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.dkoval.leetcode.challenge

import com.github.dkoval.leetcode.challenge.CountNumberOfMaximumBitwiseORSubsets.CountNumberOfMaximumBitwiseORSubsetsRev1
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.util.stream.Stream

internal class CountNumberOfMaximumBitwiseORSubsetsTest {

class InputArgumentsProvider : ArgumentsProvider {

override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of(
Arguments.of(
intArrayOf(3, 1),
2
),
Arguments.of(
intArrayOf(2, 2, 2),
7
),
Arguments.of(
intArrayOf(3, 2, 1, 5),
6
)
)
}

@Nested
inner class CountNumberOfMaximumBitwiseORSubsetsRev1Test {

@ParameterizedTest
@ArgumentsSource(InputArgumentsProvider::class)
fun `should return the number of different non-empty subsets with the maximum bitwise OR`(
nums: IntArray,
expected: Int
) {
CountNumberOfMaximumBitwiseORSubsetsRev1().test(nums, expected)
}
}
}

private fun CountNumberOfMaximumBitwiseORSubsets.test(nums: IntArray, expected: Int) {
val actual = countMaxOrSubsets(nums)
assertEquals(expected, actual)
}

0 comments on commit b230c7c

Please sign in to comment.