You are given an integer array nums of length n and an integer numSlots
such that 2 * numSlots >= n. There are numSlots slots numbered from 1 to
numSlots.
You have to place all n integers into the slots such that each slot contains at most two numbers. The AND sum of a given placement is the sum of the bitwiseAND of every number with its respective slot number.
For example, the AND sum of placing the numbers [1, 3] into slot 1 and [4, 6] into slot 2 is equal to (1 AND _1_) + (3 AND _1_) + (4 AND _2_) + (6 AND _2_) = 1 + 1 + 0 + 2 = 4.
Return _the maximum possibleAND sum of _numsgivennumSlotsslots.
Input: nums =[1,2,3,4,5,6], numSlots =3Output: 9Explanation: One possible placement is[1,4] into slot _1_ ,[2,6] into slot _2_ , and [3,5] into slot _3_.This gives the maximum AND sum of(1 AND _1_)+(4 AND _1_)+(2 AND _2_)+(6 AND _2_)+(3 AND _3_)+(5 AND _3_)=1+0+2+2+3+1=9.
Input: nums =[1,3,10,4,7,1], numSlots =9Output: 24Explanation: One possible placement is[1,1] into slot _1_ ,[3] into slot _3_ ,[4] into slot _4_ ,[7] into slot _7_ , and [10] into slot _9_.This gives the maximum AND sum of(1 AND _1_)+(1 AND _1_)+(3 AND _3_)+(4 AND _4_)+(7 AND _7_)+(10 AND _9_)=1+1+3+4+7+8=24.Note that slots 2,5,6, and 8 are empty which is permitted.
Each slot can hold at most two numbers, and the number of slots is small (≤15). We can use bitmask dynamic programming to represent the assignment of numbers to slots. For each state, we try to assign the next number to any slot that is not full, and recursively compute the maximum AND sum.
classSolution {
public:int maximumANDSum(vector<int>& nums, int numSlots) {
int n = nums.size();
int m = numSlots;
int maxMask = pow(3, m);
vector<int> dp(maxMask, 0);
for (int mask =0; mask < maxMask; ++mask) {
int cnt =0, tmp = mask;
for (int i =0; i < m; ++i) {
cnt += tmp %3;
tmp /=3;
}
if (cnt >= n) continue;
for (int i =0, t = mask; i < m; ++i, t /=3) {
if (t %3<2) {
int nmask = mask + pow(3, i);
dp[nmask] = max(dp[nmask], dp[mask] + (nums[cnt] & (i+1)));
}
}
}
return*max_element(dp.begin(), dp.end());
}
};
classSolution {
publicintmaximumANDSum(int[] nums, int numSlots) {
int n = nums.length, m = numSlots;
int maxMask = (int)Math.pow(3, m);
int[] dp =newint[maxMask];
for (int mask = 0; mask < maxMask; ++mask) {
int cnt = 0, tmp = mask;
for (int i = 0; i < m; ++i) {
cnt += tmp % 3;
tmp /= 3;
}
if (cnt >= n) continue;
for (int i = 0, t = mask; i < m; ++i, t /= 3) {
if (t % 3 < 2) {
int nmask = mask + (int)Math.pow(3, i);
dp[nmask]= Math.max(dp[nmask], dp[mask]+ (nums[cnt]& (i+1)));
}
}
}
int ans = 0;
for (int v : dp) ans = Math.max(ans, v);
return ans;
}
}
classSolution {
funmaximumANDSum(nums: IntArray, numSlots: Int): Int {
val n = nums.size
val m = numSlots
val maxMask = Math.pow(3.0, m.toDouble()).toInt()
val dp = IntArray(maxMask)
for (mask in0 until maxMask) {
var cnt = 0var tmp = mask
for (i in0 until m) {
cnt += tmp % 3 tmp /=3 }
if (cnt >= n) continuevar t = mask
for (i in0 until m) {
if (t % 3 < 2) {
val nmask = mask + Math.pow(3.0, i.toDouble()).toInt()
dp[nmask] = maxOf(dp[nmask], dp[mask] + (nums[cnt] and (i+1)))
}
t /=3 }
}
return dp.maxOrNull() ?:0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defmaximumANDSum(self, nums: list[int], numSlots: int) -> int:
from functools import lru_cache
n = len(nums)
@lru_cache(None)
defdp(i: int, mask: int) -> int:
if i == n:
return0 ans =0for slot in range(numSlots):
cnt = (mask // (3** slot)) %3if cnt <2:
nmask = mask +3** slot
ans = max(ans, (nums[i] & (slot+1)) + dp(i+1, nmask))
return ans
return dp(0, 0)