返回介绍

solution / 0800-0899 / 0805.Split Array With Same Average / README_EN

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

805. Split Array With Same Average

中文文档

Description

You are given an integer array nums.

You should move each element of nums into one of the two arrays A and B such that A and B are non-empty, and average(A) == average(B).

Return true if it is possible to achieve that and false otherwise.

Note that for an array arr, average(arr) is the sum of all the elements of arr over the length of arr.

 

Example 1:

Input: nums = [1,2,3,4,5,6,7,8]
Output: true
Explanation: We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5.

Example 2:

Input: nums = [3,1]
Output: false

 

Constraints:

  • 1 <= nums.length <= 30
  • 0 <= nums[i] <= 104

Solutions

Solution 1

class Solution:
  def splitArraySameAverage(self, nums: List[int]) -> bool:
    n = len(nums)
    if n == 1:
      return False
    s = sum(nums)
    for i, v in enumerate(nums):
      nums[i] = v * n - s
    m = n >> 1
    vis = set()
    for i in range(1, 1 << m):
      t = sum(v for j, v in enumerate(nums[:m]) if i >> j & 1)
      if t == 0:
        return True
      vis.add(t)
    for i in range(1, 1 << (n - m)):
      t = sum(v for j, v in enumerate(nums[m:]) if i >> j & 1)
      if t == 0 or (i != (1 << (n - m)) - 1 and -t in vis):
        return True
    return False
class Solution {
  public boolean splitArraySameAverage(int[] nums) {
    int n = nums.length;
    if (n == 1) {
      return false;
    }
    int s = Arrays.stream(nums).sum();
    for (int i = 0; i < n; ++i) {
      nums[i] = nums[i] * n - s;
    }
    int m = n >> 1;
    Set<Integer> vis = new HashSet<>();
    for (int i = 1; i < 1 << m; ++i) {
      int t = 0;
      for (int j = 0; j < m; ++j) {
        if (((i >> j) & 1) == 1) {
          t += nums[j];
        }
      }
      if (t == 0) {
        return true;
      }
      vis.add(t);
    }
    for (int i = 1; i < 1 << (n - m); ++i) {
      int t = 0;
      for (int j = 0; j < (n - m); ++j) {
        if (((i >> j) & 1) == 1) {
          t += nums[m + j];
        }
      }
      if (t == 0 || (i != (1 << (n - m)) - 1) && vis.contains(-t)) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  bool splitArraySameAverage(vector<int>& nums) {
    int n = nums.size();
    if (n == 1) return false;
    int s = accumulate(nums.begin(), nums.end(), 0);
    for (int& v : nums) v = v * n - s;
    int m = n >> 1;
    unordered_set<int> vis;
    for (int i = 1; i < 1 << m; ++i) {
      int t = 0;
      for (int j = 0; j < m; ++j)
        if (i >> j & 1) t += nums[j];
      if (t == 0) return true;
      vis.insert(t);
    }
    for (int i = 1; i < 1 << (n - m); ++i) {
      int t = 0;
      for (int j = 0; j < (n - m); ++j)
        if (i >> j & 1) t += nums[m + j];
      if (t == 0 || (i != (1 << (n - m)) - 1 && vis.count(-t))) return true;
    }
    return false;
  }
};
func splitArraySameAverage(nums []int) bool {
  n := len(nums)
  if n == 1 {
    return false
  }
  s := 0
  for _, v := range nums {
    s += v
  }
  for i, v := range nums {
    nums[i] = v*n - s
  }
  m := n >> 1
  vis := map[int]bool{}
  for i := 1; i < 1<<m; i++ {
    t := 0
    for j, v := range nums[:m] {
      if (i >> j & 1) == 1 {
        t += v
      }
    }
    if t == 0 {
      return true
    }
    vis[t] = true
  }
  for i := 1; i < 1<<(n-m); i++ {
    t := 0
    for j, v := range nums[m:] {
      if (i >> j & 1) == 1 {
        t += v
      }
    }
    if t == 0 || (i != (1<<(n-m))-1 && vis[-t]) {
      return true
    }
  }
  return false
}

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

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

发布评论

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