Minimum Sum of Values by Dividing Array Problem
Description
LeetCode Problem 3117.
You are given two arrays nums and andValues of length n and m respectively.
The value of an array is equal to the last element of that array.
You have to divide nums into m disjoint contiguous subarrays such that for the i^th subarray [l_i, r_i], the bitwise AND of the subarray elements is equal to andValues[i], in other words, nums[l_i] & nums[l_i + 1] & ... & nums[r_i] == andValues[i] for all 1 <= i <= m, where & represents the bitwise AND operator.
Return the minimum possible sum of the values of the m subarrays nums is divided into. If it is not possible to divide nums into m subarrays 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 nums is:
1. [1,4] as 1 & 4 == 0.
2. [3] as the bitwise AND of a single element subarray is that element itself.
3. [3] as the bitwise AND of a single element subarray is that element itself.
4. [2] as the bitwise AND of 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:
1. [[2,3,5],[7,7,7],[5]] with the sum of the values 5 + 7 + 5 == 17.
2. [[2,3,5,7],[7,7],[5]] with the sum of the values 7 + 7 + 5 == 19.
3. [[2,3,5,7,7],[7],[5]] with the sum of the values 7 + 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 AND of the entire array nums is 0. As there is no possible way to divide nums into a single subarray to have the bitwise AND of elements 2, return -1.
Constraints:
1 <= n == nums.length <= 10^41 <= m == andValues.length <= min(n, 10)1 <= nums[i] < 10^50 <= andValues[j] < 10^5
Solution
Approach: Memoized Search
Let dfs(i, j, a) be the minimum sum of subarray values obtainable from index i onward, when j subarrays are already finished and the AND of the current unfinished subarray is a (start with a = -1, all bits set). The answer is dfs(0, 0, -1).
- If fewer elements remain than subarrays still needed, return infinity.
- If all
msubarrays are done, return0wheni = n, else infinity. - Otherwise update
a &= nums[i]. Ifa < andValues[j], the AND can only shrink further, so return infinity. Else either continue the current subarray withdfs(i + 1, j, a), or, whena == andValues[j], end it here withdfs(i + 1, j + 1, -1) + nums[i]. Take the smaller.
Results are cached in a hash map. For fixed i, only about log M distinct values of a can occur, which keeps the state count small. Return -1 if the result is infinity.
Complexity: O(n m log M) time and space, where M < 10^5.
from math import inf
from functools import lru_cache
class Solution:
def minimumValueSum(self, nums: List[int], andValues: List[int]) -> int:
@lru_cache(None)
def dfs(i: int, j: int, a: int) -> int:
if n - i < m - j:
return inf
if j == m:
return 0 if i == n else inf
a &= nums[i]
if a < andValues[j]:
return inf
ans = dfs(i + 1, j, a)
if a == andValues[j]:
ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i])
return ans
n, m = len(nums), len(andValues)
ans = dfs(0, 0, -1)
return ans if ans < inf else -1