-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DP top-down solution to "Count Number of Maximum Bitwise-OR Subsets"
problem
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...t/kotlin/com/github/dkoval/leetcode/challenge/CountNumberOfMaximumBitwiseORSubsetsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |