Class Solution
java.lang.Object
g2201_2300.s2281_sum_of_total_strength_of_wizards.Solution
2281 - Sum of Total Strength of Wizards.<p>Hard</p>
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the wizards’ strengths form a <strong>subarray</strong> of <code>strength</code>), the <strong>total strength</strong> is defined as the <strong>product</strong> of the following two values:</p>
<ul>
<li>The strength of the <strong>weakest</strong> wizard in the group.</li>
<li>The <strong>total</strong> of all the individual strengths of the wizards in the group.</li>
</ul>
<p>Return <em>the <strong>sum</strong> of the total strengths of <strong>all</strong> contiguous groups of wizards</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> strength = [1,3,1,2]</p>
<p><strong>Output:</strong> 44</p>
<p><strong>Explanation:</strong> The following are all the contiguous groups of wizards:</p>
<ul>
<li>
<p>[1] from [<strong>1</strong> ,3,1,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1</p>
</li>
<li>
<p>[3] from [1, <strong>3</strong> ,1,2] has a total strength of min([3]) * sum([3]) = 3 * 3 = 9</p>
</li>
<li>
<p>[1] from [1,3, <strong>1</strong> ,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1</p>
</li>
<li>
<p>[2] from [1,3,1, <strong>2</strong> ] has a total strength of min([2]) * sum([2]) = 2 * 2 = 4</p>
</li>
<li>
<p>[1,3] from [<strong>1,3</strong> ,1,2] has a total strength of min([1,3]) * sum([1,3]) = 1 * 4 = 4</p>
</li>
<li>
<p>[3,1] from [1, <strong>3,1</strong> ,2] has a total strength of min([3,1]) * sum([3,1]) = 1 * 4 = 4</p>
</li>
<li>
<p>[1,2] from [1,3, <strong>1,2</strong> ] has a total strength of min([1,2]) * sum([1,2]) = 1 * 3 = 3</p>
</li>
<li>
<p>[1,3,1] from [<strong>1,3,1</strong> ,2] has a total strength of min([1,3,1]) * sum([1,3,1]) = 1 * 5 = 5</p>
</li>
<li>
<p>[3,1,2] from [1, <strong>3,1,2</strong> ] has a total strength of min([3,1,2]) * sum([3,1,2]) = 1 * 6 = 6</p>
</li>
<li>
<p>[1,3,1,2] from [<strong>1,3,1,2</strong> ] has a total strength of min([1,3,1,2]) * sum([1,3,1,2]) = 1 * 7 = 7</p>
</li>
</ul>
<p>The sum of all the total strengths is 1 + 9 + 1 + 4 + 4 + 4 + 3 + 5 + 6 + 7 = 44.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> strength = [5,4,6]</p>
<p><strong>Output:</strong> 213</p>
<p><strong>Explanation:</strong> The following are all the contiguous groups of wizards:</p>
<ul>
<li>
<p>[5] from [<strong>5</strong> ,4,6] has a total strength of min([5]) * sum([5]) = 5 * 5 = 25</p>
</li>
<li>
<p>[4] from [5, <strong>4</strong> ,6] has a total strength of min([4]) * sum([4]) = 4 * 4 = 16</p>
</li>
<li>
<p>[6] from [5,4, <strong>6</strong> ] has a total strength of min([6]) * sum([6]) = 6 * 6 = 36</p>
</li>
<li>
<p>[5,4] from [<strong>5,4</strong> ,6] has a total strength of min([5,4]) * sum([5,4]) = 4 * 9 = 36</p>
</li>
<li>
<p>[4,6] from [5, <strong>4,6</strong> ] has a total strength of min([4,6]) * sum([4,6]) = 4 * 10 = 40</p>
</li>
<li>
<p>[5,4,6] from [<strong>5,4,6</strong> ] has a total strength of min([5,4,6]) * sum([5,4,6]) = 4 * 15 = 60</p>
</li>
</ul>
<p>The sum of all the total strengths is 25 + 16 + 36 + 36 + 40 + 60 = 213.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strength.length <= 10<sup>5</sup></code></li>
<li><code>1 <= strength[i] <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
totalStrength
public int totalStrength(int[] nums)
-