返回介绍

solution / 2500-2599 / 2518.Number of Great Partitions / README_EN

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

2518. Number of Great Partitions

中文文档

Description

You are given an array nums consisting of positive integers and an integer k.

Partition the array into two ordered groups such that each element is in exactly one group. A partition is called great if the sum of elements of each group is greater than or equal to k.

Return _the number of distinct great partitions_. Since the answer may be too large, return it modulo 109 + 7.

Two partitions are considered distinct if some element nums[i] is in different groups in the two partitions.

 

Example 1:

Input: nums = [1,2,3,4], k = 4
Output: 6
Explanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).

Example 2:

Input: nums = [3,3,3], k = 4
Output: 0
Explanation: There are no great partitions for this array.

Example 3:

Input: nums = [6,6], k = 2
Output: 2
Explanation: We can either put nums[0] in the first partition or in the second partition.
The great partitions will be ([6], [6]) and ([6], [6]).

 

Constraints:

  • 1 <= nums.length, k <= 1000
  • 1 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def countPartitions(self, nums: List[int], k: int) -> int:
    if sum(nums) < k * 2:
      return 0
    mod = 10**9 + 7
    n = len(nums)
    f = [[0] * k for _ in range(n + 1)]
    f[0][0] = 1
    ans = 1
    for i in range(1, n + 1):
      ans = ans * 2 % mod
      for j in range(k):
        f[i][j] = f[i - 1][j]
        if j >= nums[i - 1]:
          f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod
    return (ans - sum(f[-1]) * 2 + mod) % mod
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int countPartitions(int[] nums, int k) {
    long s = 0;
    for (int v : nums) {
      s += v;
    }
    if (s < k * 2) {
      return 0;
    }
    int n = nums.length;
    long[][] f = new long[n + 1][k];
    f[0][0] = 1;
    long ans = 1;
    for (int i = 1; i <= n; ++i) {
      int v = nums[i - 1];
      ans = ans * 2 % MOD;
      for (int j = 0; j < k; ++j) {
        f[i][j] = f[i - 1][j];
        if (j >= v) {
          f[i][j] = (f[i][j] + f[i - 1][j - v]) % MOD;
        }
      }
    }
    for (int j = 0; j < k; ++j) {
      ans = (ans - f[n][j] * 2 % MOD + MOD) % MOD;
    }
    return (int) ans;
  }
}
class Solution {
public:
  const int mod = 1e9 + 7;

  int countPartitions(vector<int>& nums, int k) {
    long s = accumulate(nums.begin(), nums.end(), 0l);
    if (s < k * 2) return 0;
    int n = nums.size();
    long f[n + 1][k];
    int ans = 1;
    memset(f, 0, sizeof f);
    f[0][0] = 1;
    for (int i = 1; i <= n; ++i) {
      int v = nums[i - 1];
      ans = ans * 2 % mod;
      for (int j = 0; j < k; ++j) {
        f[i][j] = f[i - 1][j];
        if (j >= v) {
          f[i][j] = (f[i][j] + f[i - 1][j - v]) % mod;
        }
      }
    }
    for (int j = 0; j < k; ++j) {
      ans = (ans - f[n][j] * 2 % mod + mod) % mod;
    }
    return ans;
  }
};
func countPartitions(nums []int, k int) int {
  s := 0
  for _, v := range nums {
    s += v
  }
  if s < k*2 {
    return 0
  }
  const mod int = 1e9 + 7
  n := len(nums)
  f := make([][]int, n+1)
  for i := range f {
    f[i] = make([]int, k)
  }
  f[0][0] = 1
  ans := 1
  for i := 1; i <= n; i++ {
    v := nums[i-1]
    ans = ans * 2 % mod
    for j := 0; j < k; j++ {
      f[i][j] = f[i-1][j]
      if j >= v {
        f[i][j] = (f[i][j] + f[i-1][j-v]) % mod
      }
    }
  }
  for j := 0; j < k; j++ {
    ans = (ans - f[n][j]*2%mod + mod) % mod
  }
  return ans
}

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

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

发布评论

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