2681. Power of Heroes Hard

@problem@discussion
#Array#Math#Dynamic Programming#Sorting#Prefix Sum



1/**
2 * [2681] Power of Heroes
3 *
4 * 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:
5 * 
6 * 	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]).
7 * 
8 * Return the sum of the power of all non-empty groups of heroes possible. Since the sum could be very large, return it modulo 10^9 + 7.
9 *  
10 * <strong class="example">Example 1:
11 * 
12 * Input: nums = [2,1,4]
13 * Output: 141
14 * Explanation: 
15 * 1^st group: [2] has power = 2^2 * 2 = 8.
16 * 2^nd group: [1] has power = 1^2 * 1 = 1. 
17 * 3^rd group: [4] has power = 4^2 * 4 = 64. 
18 * 4^th group: [2,1] has power = 2^2 * 1 = 4. 
19 * 5^th group: [2,4] has power = 4^2 * 2 = 32. 
20 * 6^th group: [1,4] has power = 4^2 * 1 = 16. 
21 * ​​​​​​​7^th group: [2,1,4] has power = 4^2​​​​​​​ * 1 = 16. 
22 * The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.
23 * 
24 * <strong class="example">Example 2:
25 * 
26 * Input: nums = [1,1,1]
27 * Output: 7
28 * 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.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= nums.length <= 10^5
34 * 	1 <= nums[i] <= 10^9
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/power-of-heroes/
40// discuss: https://leetcode.com/problems/power-of-heroes/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn sum_of_power(nums: Vec<i32>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2681() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.