Class Solution
-
- All Implemented Interfaces:
public final class Solution3117 - Minimum Sum of Values by Dividing Array.
Hard
You are given two arrays
numsandandValuesof lengthnandmrespectively.The value of an array is equal to the last element of that array.
You have to divide
numsintomdisjoint contiguous subarrays such that for the <code>i<sup>th</sup></code> subarray <code>l<sub>i</sub>, r<sub>i</sub></code>, the bitwiseANDof the subarray elements is equal toandValues[i], in other words, <code>numsl<sub>i</sub>& numsl<sub>i</sub> + 1& ... & numsr<sub>i</sub> == andValuesi</code> for all1 <= i <= m, where&represents the bitwiseANDoperator.Return the minimum possible sum of the values of the
msubarraysnumsis divided into. If it is not possible to dividenumsintomsubarrays satisfying these conditions, return-1.Example 1:
Input: nums = 1,4,3,3,2, andValues = 0,3,3,2
Output: 12
Explanation:
The only possible way to divide
numsis:[1,4]as1 & 4 == 0.[3]as the bitwiseANDof a single element subarray is that element itself.[3]as the bitwiseANDof a single element subarray is that element itself.[2]as the bitwiseANDof a single element subarray is that element itself.
The sum of the values for these subarrays is
4 + 3 + 3 + 2 = 12.Example 2:
Input: nums = 2,3,5,7,7,7,5, andValues = 0,7,5
Output: 17
Explanation:
There are three ways to divide
nums:[[2,3,5],[7,7,7],[5]]with the sum of the values5 + 7 + 5 == 17.[[2,3,5,7],[7,7],[5]]with the sum of the values7 + 7 + 5 == 19.[[2,3,5,7,7],[7],[5]]with the sum of the values7 + 7 + 5 == 19.
The minimum possible sum of the values is
17.Example 3:
Input: nums = 1,2,3,4, andValues = 2
Output: \-1
Explanation:
The bitwise
ANDof the entire arraynumsis0. As there is no possible way to dividenumsinto a single subarray to have the bitwiseANDof elements2, return-1.Constraints:
<code>1 <= n == nums.length <= 10<sup>4</sup></code>
1 <= m == andValues.length <= min(n, 10)<code>1 <= numsi< 10<sup>5</sup></code>
<code>0 <= andValuesj< 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminimumValueSum(IntArray nums, IntArray andValues)-
-
Method Detail
-
minimumValueSum
final Integer minimumValueSum(IntArray nums, IntArray andValues)
-
-
-
-