返回介绍

solution / 2600-2699 / 2681.Power of Heroes / README_EN

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

2681. Power of Heroes

中文文档

Description

You are given a 0-indexed integer array nums representing the strength of some heroes. The power of a group of heroes is defined as follows:

  • Let i0, i1, ... ,ik be the indices of the heroes in a group. Then, the power of this group is max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik]).

Return _the sum of the power of all non-empty groups of heroes possible._ Since the sum could be very large, return it modulo 109 + 7.

 

Example 1:

Input: nums = [2,1,4]
Output: 141
Explanation: 
1st group: [2] has power = 22 * 2 = 8.
2nd group: [1] has power = 12 * 1 = 1. 
3rd group: [4] has power = 42 * 4 = 64. 
4th group: [2,1] has power = 22 * 1 = 4. 
5th group: [2,4] has power = 42 * 2 = 32. 
6th group: [1,4] has power = 42 * 1 = 16. 
​​​​​​​7th group: [2,1,4] has power = 42​​​​​​​ * 1 = 16. 
The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.

Example 2:

Input: nums = [1,1,1]
Output: 7
Explanation: A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def sumOfPower(self, nums: List[int]) -> int:
    mod = 10**9 + 7
    nums.sort()
    ans = 0
    p = 0
    for x in nums[::-1]:
      ans = (ans + (x * x % mod) * x) % mod
      ans = (ans + x * p) % mod
      p = (p * 2 + x * x) % mod
    return ans
class Solution {
  public int sumOfPower(int[] nums) {
    final int mod = (int) 1e9 + 7;
    Arrays.sort(nums);
    long ans = 0, p = 0;
    for (int i = nums.length - 1; i >= 0; --i) {
      long x = nums[i];
      ans = (ans + (x * x % mod) * x) % mod;
      ans = (ans + x * p % mod) % mod;
      p = (p * 2 + x * x % mod) % mod;
    }
    return (int) ans;
  }
}
class Solution {
public:
  int sumOfPower(vector<int>& nums) {
    const int mod = 1e9 + 7;
    sort(nums.rbegin(), nums.rend());
    long long ans = 0, p = 0;
    for (long long x : nums) {
      ans = (ans + (x * x % mod) * x) % mod;
      ans = (ans + x * p % mod) % mod;
      p = (p * 2 + x * x % mod) % mod;
    }
    return ans;
  }
};
func sumOfPower(nums []int) (ans int) {
  const mod = 1e9 + 7
  sort.Ints(nums)
  p := 0
  for i := len(nums) - 1; i >= 0; i-- {
    x := nums[i]
    ans = (ans + (x*x%mod)*x) % mod
    ans = (ans + x*p%mod) % mod
    p = (p*2 + x*x%mod) % mod
  }
  return
}
function sumOfPower(nums: number[]): number {
  const mod = 10 ** 9 + 7;
  nums.sort((a, b) => a - b);
  let ans = 0;
  let p = 0;
  for (let i = nums.length - 1; i >= 0; --i) {
    const x = BigInt(nums[i]);
    ans = (ans + Number((x * x * x) % BigInt(mod))) % mod;
    ans = (ans + Number((x * BigInt(p)) % BigInt(mod))) % mod;
    p = Number((BigInt(p) * 2n + x * x) % BigInt(mod));
  }
  return ans;
}

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

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

发布评论

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