返回介绍

solution / 2100-2199 / 2172.Maximum AND Sum of Array / README_EN

发布于 2024-06-17 01:03:09 字数 5590 浏览 0 评论 0 收藏 0

2172. Maximum AND Sum of Array

中文文档

Description

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 bitwise AND 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 possible AND sum of _nums_ given _numSlots_ slots._

 

Example 1:

Input: nums = [1,2,3,4,5,6], numSlots = 3
Output: 9
Explanation: 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.

Example 2:

Input: nums = [1,3,10,4,7,1], numSlots = 9
Output: 24
Explanation: 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.

 

Constraints:

  • n == nums.length
  • 1 <= numSlots <= 9
  • 1 <= n <= 2 * numSlots
  • 1 <= nums[i] <= 15

Solutions

Solution 1

class Solution:
  def maximumANDSum(self, nums: List[int], numSlots: int) -> int:
    n = len(nums)
    m = numSlots << 1
    f = [0] * (1 << m)
    for i in range(1 << m):
      cnt = i.bit_count()
      if cnt > n:
        continue
      for j in range(m):
        if i >> j & 1:
          f[i] = max(f[i], f[i ^ (1 << j)] + (nums[cnt - 1] & (j // 2 + 1)))
    return max(f)
class Solution {
  public int maximumANDSum(int[] nums, int numSlots) {
    int n = nums.length;
    int m = numSlots << 1;
    int[] f = new int[1 << m];
    int ans = 0;
    for (int i = 0; i < 1 << m; ++i) {
      int cnt = Integer.bitCount(i);
      if (cnt > n) {
        continue;
      }
      for (int j = 0; j < m; ++j) {
        if ((i >> j & 1) == 1) {
          f[i] = Math.max(f[i], f[i ^ (1 << j)] + (nums[cnt - 1] & (j / 2 + 1)));
        }
      }
      ans = Math.max(ans, f[i]);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumANDSum(vector<int>& nums, int numSlots) {
    int n = nums.size();
    int m = numSlots << 1;
    int f[1 << m];
    memset(f, 0, sizeof(f));
    for (int i = 0; i < 1 << m; ++i) {
      int cnt = __builtin_popcount(i);
      if (cnt > n) {
        continue;
      }
      for (int j = 0; j < m; ++j) {
        if (i >> j & 1) {
          f[i] = max(f[i], f[i ^ (1 << j)] + (nums[cnt - 1] & (j / 2 + 1)));
        }
      }
    }
    return *max_element(f, f + (1 << m));
  }
};
func maximumANDSum(nums []int, numSlots int) int {
  n := len(nums)
  m := numSlots << 1
  f := make([]int, 1<<m)
  for i := range f {
    cnt := bits.OnesCount(uint(i))
    if cnt > n {
      continue
    }
    for j := 0; j < m; j++ {
      if i>>j&1 == 1 {
        f[i] = max(f[i], f[i^(1<<j)]+(nums[cnt-1]&(j/2+1)))
      }
    }
  }
  return slices.Max(f)
}
function maximumANDSum(nums: number[], numSlots: number): number {
  const n = nums.length;
  const m = numSlots << 1;
  const f: number[] = new Array(1 << m).fill(0);
  for (let i = 0; i < 1 << m; ++i) {
    const cnt = i
      .toString(2)
      .split('')
      .filter(c => c === '1').length;
    if (cnt > n) {
      continue;
    }
    for (let j = 0; j < m; ++j) {
      if (((i >> j) & 1) === 1) {
        f[i] = Math.max(f[i], f[i ^ (1 << j)] + (nums[cnt - 1] & ((j >> 1) + 1)));
      }
    }
  }
  return Math.max(...f);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文