Class Solution

java.lang.Object
g2601_2700.s2681_power_of_heroes.Solution

public class Solution extends Object
2681 - Power of Heroes.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the strength of some heroes. The <strong>power</strong> of a group of heroes is defined as follows:</p> <ul> <li>Let <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, &hellip; ,<code>i<sub>k</sub></code> be the indices of the heroes in a group. Then, the power of this group is <code>max(nums[i<sub>0</sub>], nums[i<sub>1</sub>], &hellip; ,nums[i<sub>k</sub>])<sup>2</sup> * min(nums[i<sub>0</sub>], nums[i<sub>1</sub>], &hellip; ,nums[i<sub>k</sub>])</code>.</li> </ul> <p>Return <em>the sum of the <strong>power</strong> of all <strong>non-empty</strong> groups of heroes possible.</em> Since the sum could be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,1,4]</p> <p><strong>Output:</strong> 141</p> <p><strong>Explanation:</strong></p> <p>1<sup>st</sup> group: [2] has power = 2<sup>2</sup> * 2 = 8.</p> <p>2<sup>nd</sup> group: [1] has power = 1<sup>2</sup> * 1 = 1.</p> <p>3<sup>rd</sup> group: [4] has power = 4<sup>2</sup> * 4 = 64.</p> <p>4<sup>th</sup> group: [2,1] has power = 2<sup>2</sup> * 1 = 4.</p> <p>5<sup>th</sup> group: [2,4] has power = 4<sup>2</sup> * 2 = 32.</p> <p>6<sup>th</sup> group: [1,4] has power = 4<sup>2</sup> * 1 = 16.</p> <p>7<sup>th</sup> group: [2,1,4] has power = 4<sup>2</sup> * 1 = 16.</p> <p>The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,1,1]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sumOfPower

      public int sumOfPower(int[] nums)