返回介绍

solution / 2800-2899 / 2862.Maximum Element-Sum of a Complete Subset of Indices / README_EN

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

2862. Maximum Element-Sum of a Complete Subset of Indices

中文文档

Description

You are given a 1-indexed array nums of n integers.

A set of numbers is complete if the product of every pair of its elements is a perfect square.

For a subset of the indices set {1, 2, ..., n} represented as {i1, i2, ..., ik}, we define its element-sum as: nums[i1] + nums[i2] + ... + nums[ik].

Return _the maximum element-sum of a complete subset of the indices set_ {1, 2, ..., n}.

A perfect square is a number that can be expressed as the product of an integer by itself.

 

Example 1:

Input: nums = [8,7,3,5,7,2,4,9]
Output: 16
Explanation: Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}.
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13.
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16.
Hence, the maximum element-sum of a complete subset of indices is 16.

Example 2:

Input: nums = [5,10,3,10,1,13,7,9,4]
Output: 19
Explanation: Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}.
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15.
The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9.
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19.
The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14.
The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19.
Hence, the maximum element-sum of a complete subset of indices is 19.

 

Constraints:

  • 1 <= n == nums.length <= 104
  • 1 <= nums[i] <= 109

Solutions

Solution 1: Enumeration

We note that if a number can be expressed in the form of $k \times j^2$, then all numbers of this form have the same $k$.

Therefore, we can enumerate $k$ in the range $[1,..n]$, and then start enumerating $j$ from $1$, each time adding the value of $nums[k \times j^2 - 1]$ to $t$, until $k \times j^2 > n$. At this point, update the answer to $ans = \max(ans, t)$.

Finally, return the answer $ans$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def maximumSum(self, nums: List[int]) -> int:
    n = len(nums)
    ans = 0
    for k in range(1, n + 1):
      t = 0
      j = 1
      while k * j * j <= n:
        t += nums[k * j * j - 1]
        j += 1
      ans = max(ans, t)
    return ans
class Solution {
  public long maximumSum(List<Integer> nums) {
    long ans = 0;
    int n = nums.size();
    boolean[] used = new boolean[n + 1];
    int bound = (int) Math.floor(Math.sqrt(n));
    int[] squares = new int[bound + 1];
    for (int i = 1; i <= bound + 1; i++) {
      squares[i - 1] = i * i;
    }
    for (int i = 1; i <= n; i++) {
      long res = 0;
      int idx = 0;
      int curr = i * squares[idx];
      while (curr <= n) {
        res += nums.get(curr - 1);
        curr = i * squares[++idx];
      }
      ans = Math.max(ans, res);
    }
    return ans;
  }
}
class Solution {
  public long maximumSum(List<Integer> nums) {
    long ans = 0;
    int n = nums.size();
    for (int k = 1; k <= n; ++k) {
      long t = 0;
      for (int j = 1; k * j * j <= n; ++j) {
        t += nums.get(k * j * j - 1);
      }
      ans = Math.max(ans, t);
    }
    return ans;
  }
}
class Solution {
public:
  long long maximumSum(vector<int>& nums) {
    long long ans = 0;
    int n = nums.size();
    for (int k = 1; k <= n; ++k) {
      long long t = 0;
      for (int j = 1; k * j * j <= n; ++j) {
        t += nums[k * j * j - 1];
      }
      ans = max(ans, t);
    }
    return ans;
  }
};
func maximumSum(nums []int) (ans int64) {
  n := len(nums)
  for k := 1; k <= n; k++ {
    var t int64
    for j := 1; k*j*j <= n; j++ {
      t += int64(nums[k*j*j-1])
    }
    ans = max(ans, t)
  }
  return
}
function maximumSum(nums: number[]): number {
  let ans = 0;
  const n = nums.length;
  for (let k = 1; k <= n; ++k) {
    let t = 0;
    for (let j = 1; k * j * j <= n; ++j) {
      t += nums[k * j * j - 1];
    }
    ans = Math.max(ans, t);
  }
  return ans;
}

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

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

发布评论

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