LeetCode in Kotlin

3117. Minimum Sum of Values by Dividing Array

Hard

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 ith subarray [li, ri], the bitwise AND of the subarray elements is equal to andValues[i], in other words, nums[li] & nums[li + 1] & ... & nums[ri] == 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:

Solution

import kotlin.math.min

class Solution {
    fun minimumValueSum(nums: IntArray, andValues: IntArray): Int {
        val n = nums.size
        var dp = IntArray(n + 1)
        dp.fill(INF)
        dp[0] = 0
        for (target in andValues) {
            var sum = INF
            var minSum = INF
            var rightSum = INF
            val leftSum = IntArray(n + 1)
            leftSum[0] = INF
            var left = 0
            var right = 0
            val nextdp = IntArray(n + 1)
            nextdp[0] = INF
            for (i in 0 until n) {
                sum = sum and nums[i]
                rightSum = rightSum and nums[i]
                ++right
                if (sum < target) {
                    minSum = INF
                    sum = nums[i]
                }
                while ((leftSum[left] and rightSum) <= target) {
                    if ((leftSum[left] and rightSum) == target) {
                        minSum = min(minSum, dp[i - left - right + 1])
                    }
                    if (left-- > 0) {
                        continue
                    }
                    left = right
                    var start = i
                    for (l in 1..left) {
                        leftSum[l] = leftSum[l - 1] and nums[start--]
                    }
                    right = 0
                    rightSum = INF
                }
                nextdp[i + 1] = minSum + nums[i]
            }
            dp = nextdp
        }
        return if (dp[n] < INF) dp[n] else -1
    }

    companion object {
        private const val INF = 0xfffffff
    }
}