【Likou】2681. The Power of Heroes

The following are other people's solution ideas, and my own code

topic

Title meaning

gives you a subscript from 0 00- based integer arraynums numsn u m s , which represents the hero's ability value. If we select some heroes,the poweris defined as:
i 0 , i 1 , . . . ik i_0 , i_1 ,... i_ki0i1...ikIndicates the subscript of this group of heroes in the array. Then the strength of this group of heroes is max ( nums [ i 0 ] , nums [ i 1 ] . . . nums [ ik ] ) 2 ∗ min ( nums [ i 0 ] , nums [ i 1 ] . . . nums [ ik ] ) max(nums[i_0],nums[i_1] ... nums[i_k])2 * min(nums[i_0],nums[i_1] ... nums[i_k])max(nums[i0],nums[i1]...nums[ik])2min(nums[i0],nums[i1]...nums[ik]) . Please return the sum of the power
all possiblenon-emptyhero groups. Since the answer may be very large, please compare the result to1 0 9 + 7 10^9 + 7109+7 to take the remainder.

Example 1

Input : nums = [ 2 , 1 , 4 ] nums = [2,1,4]n u m s=[2,1,4 ]
Output:141 141141
Explanation:
No.1 1Group 1 :[ 2 ] [2]The power of [ 2 ] is2 2 ∗ 2 = 8 2^2 * 2 = 8222=8 .
Second2Group 2 : [ 1 ] [1][ 1 ] has a power of1 2 ∗ 1 = 1 1^2 * 1 = 1121=1 .
3rd3rd3 groups:[ 4 ] [4]The power of [ 4 ] is4 2 ∗ 4 = 64 4^2 * 4 = 64424=64 .
4th4th4 groups:[ 2 , 1 ] [2,1][2,1 ] has a power of2 2 ∗ 1 = 4 2^2 * 1 = 4221=4 .
5th5th5 groups:[ 2 , 4 ] [2,4][2,4 ] has a power of4 2 ∗ 2 = 32 4^2 * 2 = 32422=32 .
6th6th6 groups:[ 1 , 4 ] [1,4][1,4 ] has a power of4 2 ∗ 1 = 16 4^2 * 1 = 16421=16 .
​​​​​​7 groups:[ 2 , 1 , 4 ] [2,1,4][2,1,4 ] has a power of4 2​​​​​​​​ ∗ 1 = 16 4^2​​​​​​​​ * 1 = 1642​​​​​​​1=16 .
The sum of the power of all hero groups is8 + 1 + 64 + 4 + 32 + 16 + 16 = 141 8 + 1 + 64 + 4 + 32 + 16 + 16 = 1418+1+64+4+32+16+16=141

Example 2

Input : nums = [ 1 , 1 , 1 ] nums = [1,1,1]n u m s=[1,1,1 ]
Output:7 77
Explanation: There are7 in total 77 hero groups, the power of each group is1 11 . So the sum of the power of all hero groups is7 77

hint

  1. 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
  2. 1 < = n u m s [ i ] < = 1 0 9 1 <= nums[i] <= 10^9 1<=nums[i]<=109

other people's ideas

contribution method

Algorithm summary

Since the order of the elements does not affect the answer, sort first.

设有 a , b , c , d , e a,b,c,d,e a,b,c,d,e five numbers, in ascending order.

If put ddd as the maximum value:

  • If only choose ddd is a single number, then the power isd 3 d^3d3
  • Choose aaa is the minimum value, due to the middlebbb andccc is optional, there are a total of2 2 2^222 schemes, so the sum of power isd 2 ∗ a ∗ 2 2 d^2 * a * 2^2d2a22
  • choose bbb is the minimum value due to the middleccc is optional, there are a total of2 1 2^121 scheme, so the sum of forces isd 2 ∗ b ∗ 2 1 d^2 * b * 2^1d2b21
  • choose ccc is the minimum value, only2 0 = 1 2^0 = 120=1 scheme, so the sum of forces isd 2 ⋅ c ⋅ 2 0 d^2⋅c⋅2^0d2c20

Therefore, when ddWhen d is the maximum value, ddThe contribution of d and its left elements to the answer is
d 3 + d 2 ∗ ( a ∗ 2 2 + b ∗ 2 1 + c ∗ 2 0 ) d^3 + d^2 * (a * 2^2 + b * 2^1 + c * 2^0)d3+d2(a22+b21+c20)

s = a ∗ 2 2 + b ∗ 2 1 + c ∗ 2 0 s = a * 2^2 + b * 2^1 + c * 2^0 s=a22+b21+c20,上式的
d 3 + d 2 ∗ s = d 2 ∗ ( d + s ) d^3 + d^2 * s = d^2 * (d + s)d3+d2s=d2(d+s)

Go ahead and put eee as the maximum value, observessHow s changes, that is,a , b , c , da,b,c,da,b,c,Contribution of d as minimum:
a ∗ 2 3 + b ∗ 2 3 + c ∗ 2 1 + d ∗ 2 0 = 2 ∗ ( a ∗ 2 2 + b ∗ 2 1 + c ∗ 2 0 ) + d ∗ 2 0 = 2 ∗ s + da * 2^3 + b * 2^3 + c * 2^1 + d * 2^0 \\= 2 * (a * 2^2 + b * 2^1 + c * 2^ 0) + d * 2^0 \\= 2 * s + da23+b23+c21+d20=2(a22+b21+c20)+d20=2s+d

This means that we don't need to enumerate the minimum value, we only need to enumerate the maximum value, we can put sss is recursively calculated.

the complexity

  • Time Complexity: O ( nlog ⁡ n ) O(nlog⁡n)O ( n l o g n ) , of whichnnn n u m s nums The length of n u m s . The bottleneck is sorting.
  • Space complexity: O ( 1 ) O(1)O ( 1 ) . Ignoring stack space for sorting, only a few extra variables are used.

My code

Java

class Solution {
    
    
    public int sumOfPower(int[] nums) {
    
    
        Arrays.sort(nums);

        long mod = (long) 1e9 + 7;
        long ans = 0, s = 0;
        for (long i : nums) {
    
    
            ans = (ans + i * i % mod * (i + s)) % mod;
            s = (2 * s + i) % mod;
        }

        return (int) ans;
    }
}

Submission result: passed

  • Execution time: 14 ms 14ms14ms
  • Memory Consumption: 51.72 MB 51.72MB51.72 MB

Guess you like

Origin blog.csdn.net/qq_45256357/article/details/132051558