返回介绍

solution / 2800-2899 / 2834.Find the Minimum Possible Sum of a Beautiful Array / README_EN

发布于 2024-06-17 01:02:59 字数 5538 浏览 0 评论 0 收藏 0

2834. Find the Minimum Possible Sum of a Beautiful Array

中文文档

Description

You are given positive integers n and target.

An array nums is beautiful if it meets the following conditions:

  • nums.length == n.
  • nums consists of pairwise distinct positive integers.
  • There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target.

Return _the minimum possible sum that a beautiful array could have modulo _109 + 7.

 

Example 1:

Input: n = 2, target = 3
Output: 4
Explanation: We can see that nums = [1,3] is beautiful.
- The array nums has length n = 2.
- The array nums consists of pairwise distinct positive integers.
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
It can be proven that 4 is the minimum possible sum that a beautiful array could have.

Example 2:

Input: n = 3, target = 3
Output: 8
Explanation: We can see that nums = [1,3,4] is beautiful.
- The array nums has length n = 3.
- The array nums consists of pairwise distinct positive integers.
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
It can be proven that 8 is the minimum possible sum that a beautiful array could have.

Example 3:

Input: n = 1, target = 1
Output: 1
Explanation: We can see, that nums = [1] is beautiful.

 

Constraints:

  • 1 <= n <= 109
  • 1 <= target <= 109

Solutions

Solution 1: Greedy + Mathematics

We can greedily construct the array nums starting from $x = 1$, choosing $x$ each time and excluding $target - x$.

Let's denote $m = \left\lfloor \frac{target}{2} \right\rfloor$.

If $x <= m$, then the numbers we can choose are $1, 2, \cdots, n$, so the sum of the array is $\left\lfloor \frac{(1+n)n}{2} \right\rfloor$.

If $x > m$, then the numbers we can choose are $1, 2, \cdots, m$, a total of $m$ numbers, and $n - m$ numbers starting from $target$, so the sum of the array is $\left\lfloor \frac{(1+m)m}{2} \right\rfloor + \left\lfloor \frac{(target + target + n - m - 1)(n-m)}{2} \right\rfloor$.

Note that we need to take the modulus of $10^9 + 7$ for the result.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def minimumPossibleSum(self, n: int, target: int) -> int:
    mod = 10**9 + 7
    m = target // 2
    if n <= m:
      return ((1 + n) * n // 2) % mod
    return ((1 + m) * m // 2 + (target + target + n - m - 1) * (n - m) // 2) % mod
class Solution {
  public int minimumPossibleSum(int n, int target) {
    final int mod = (int) 1e9 + 7;
    int m = target / 2;
    if (n <= m) {
      return (int) ((1L + n) * n / 2 % mod);
    }
    long a = (1L + m) * m / 2 % mod;
    long b = ((1L * target + target + n - m - 1) * (n - m) / 2) % mod;
    return (int) ((a + b) % mod);
  }
}
class Solution {
public:
  int minimumPossibleSum(int n, int target) {
    const int mod = 1e9 + 7;
    int m = target / 2;
    if (n <= m) {
      return (1LL + n) * n / 2 % mod;
    }
    long long a = (1LL + m) * m / 2 % mod;
    long long b = (1LL * target + target + n - m - 1) * (n - m) / 2 % mod;
    return (a + b) % mod;
  }
};
func minimumPossibleSum(n int, target int) int {
  const mod int = 1e9 + 7
  m := target / 2
  if n <= m {
    return (n + 1) * n / 2 % mod
  }
  a := (m + 1) * m / 2 % mod
  b := (target + target + n - m - 1) * (n - m) / 2 % mod
  return (a + b) % mod
}
function minimumPossibleSum(n: number, target: number): number {
  const mod = 10 ** 9 + 7;
  const m = target >> 1;
  if (n <= m) {
    return (((1 + n) * n) / 2) % mod;
  }
  return (((1 + m) * m) / 2 + ((target + target + n - m - 1) * (n - m)) / 2) % mod;
}
public class Solution {
  public int MinimumPossibleSum(int n, int target) {
    const int mod = (int) 1e9 + 7;
    int m = target / 2;
    if (n <= m) {
      return (int) ((1L + n) * n / 2 % mod);
    }
    long a = (1L + m) * m / 2 % mod;
    long b = ((1L * target + target + n - m - 1) * (n - m) / 2) % mod;
    return (int) ((a + b) % mod);
  }
}

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

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

发布评论

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