907:子数组的最小值之和
This commit is contained in:
parent
25db7ef69c
commit
3bafe3e458
@ -0,0 +1,70 @@
|
||||
//给定一个整数数组 arr,找到 min(b) 的总和,其中 b 的范围为 arr 的每个(连续)子数组。
|
||||
//
|
||||
// 由于答案可能很大,因此 返回答案模 10^9 + 7 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:arr = [3,1,2,4]
|
||||
//输出:17
|
||||
//解释:
|
||||
//子数组为 [3],[1],[2],[4],[3,1],[1,2],[2,4],[3,1,2],[1,2,4],[3,1,2,4]。
|
||||
//最小值为 3,1,2,4,1,1,2,1,1,1,和为 17。
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:arr = [11,81,94,43,3]
|
||||
//输出:444
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= arr.length <= 3 * 104
|
||||
// 1 <= arr[i] <= 3 * 104
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 栈 数组
|
||||
// 👍 227 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//907:子数组的最小值之和
|
||||
public class SumOfSubarrayMinimums {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SumOfSubarrayMinimums().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int sumSubarrayMins(int[] arr) {
|
||||
int result = 0;
|
||||
int length = arr.length;
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int i = 0; i <= length; i++) {
|
||||
int num = (i == length) ? 0 : arr[i];
|
||||
while (!stack.empty() && num < arr[stack.peek()]) {
|
||||
int index = stack.pop();
|
||||
int left = index - (stack.empty() ? -1 : stack.peek());
|
||||
int right = i - index;
|
||||
result = (int) ((result + (long) left * right * arr[index]) % (Math.pow(10, 9) + 7));
|
||||
}
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<p>给定一个整数数组 <code>arr</code>,找到 <code>min(b)</code> 的总和,其中 <code>b</code> 的范围为 <code>arr</code> 的每个(连续)子数组。</p>
|
||||
|
||||
<p>由于答案可能很大,因此<strong> 返回答案模 <code>10^9 + 7</code></strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [3,1,2,4]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:
|
||||
</strong>子数组为<strong> </strong>[3],[1],[2],[4],[3,1],[1,2],[2,4],[3,1,2],[1,2,4],[3,1,2,4]。
|
||||
最小值为 3,1,2,4,1,1,2,1,1,1,和为 17。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [11,81,94,43,3]
|
||||
<strong>输出:</strong>444
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 3 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>数组</li></div></div>\n<div><li>👍 227</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user