【LeetCode】 907 子数组的最小值之和
Decrisption
Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo \(10^9\) + 7.
Input
Example 1:
Input: \(arr = [3,1,2,4]\)
Output: 17
Explanation:
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Sum is 17.
Example 2:
Input: \(arr = [11,81,94,43,3]\)
Output: 444
Constraints
\(1\leq arr.length\leq3*10^4\)
\(1\leq arr[i]\leq3*10^4\)
Solution
思路:
方法一:暴力计算
直接计算每一个子数组中的最小值并全部相加,时间复杂度为\(O\)(\(n^2\)),显然会超时。
class Solution {
public:
int MOD=1e9+7;
int sumSubarrayMins(vector<int>& arr) {
int n=arr.size();
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
int min=*min_element(arr.begin()+j,arr.begin()+i+1);
ans+=min;
ans%=MOD;
}
}
return ans;
}
};
方法二:两次单调栈+贡献法
方法一中由于重复计算了许多次相同的最小值,所以我们要想办法免去复杂计算。对于数组中的每一个元素,其都会在最终的答案中有一个贡献值。该贡献值可以看成是每一个元素作为子数组的最小元素的次数。
注意到:对于某一个元素\(x\),假定其下标为\(i\),如果x是某一个子数组的最小值,该子数组的左端点为\(left\),右端点为\(right\),那么在[\(left\),\(right\)]范围内所有包含\(x\)的子数组的最小值都为x,x的贡献为\(arr[i]\)\(*\)(\(i-left\))\(*\)(\(right-i\))。所以我们要找到每一个元素左侧第一个小于该元素的位置\(left\)和右侧第一个小于该元素的位置\(right\)。这样在区间\((left,right)\)范围内包含\(x\)的子数组的最小值可以确定为x,x的贡献值为\(arr[i]*(i-left)*(right-i)\).
class Solution {
public:
int MOD=1e9+7;
typedef long long ll;
int sumSubarrayMins(vector<int>& arr) {
int n=arr.size();
ll ans=0;
vector<ll> left(n,-1); //左边界为-1
vector<ll> right(n,n); //右边界为n
stack<ll> stk;
for(int i=0;i<n;i++){
while(!stk.empty()&&arr[stk.top()]>arr[i]){
stk.pop();
}
if(!stk.empty()){
left[i]=stk.top();
}
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i=n-1;i>=0;i--){
while(!stk.empty()&&arr[stk.top()]>=arr[i]){
stk.pop();
}
if(!stk.empty()){
right[i]=stk.top();
}
stk.push(i);
}
for(ll i=0;i<n;i++){
ans+= (ll)arr[i]*(i-left[i])*(right[i]-i);
ans%=MOD;
}
return ans;
}
};
时间复杂度:\(O(n)\)
空间复杂度:\(O(n)\)
【LeetCode】 907 子数组的最小值之和的更多相关文章
- [Swift]LeetCode907. 子数组的最小值之和 | Sum of Subarray Minimums
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- Leetcode 643.子数组最大平均数I
子数组最大平均数I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Java实现 LeetCode 643 子数组最大平均数 I(滑动窗口)
643. 子数组最大平均数 I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.7 ...
- [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- N元数组的子数组之和的最大值
题目:有N个整数的元素的一维数组,求子数组中元素之和中最大的一组(思想:动态规划) 分析: 设该数组为array[N], 那么对于array[i]该不该在元素之和最大的那个子数组中呢?首先,不如假设a ...
- 152.Maximum Product Subarray---dp---连续子数组的最大乘积---《编程之美》2.13子数组的最大乘积
题目链接:https://leetcode.com/problems/maximum-product-subarray/description/ 题目大意:给出一串数组,找出连续子数组中乘积最大的子数 ...
- 累加和为 K 的子数组问题
累加和为 K 的子数组问题 作者:Grey 原文地址: 博客园:累加和为 K 的子数组问题 CSDN:累加和为 K 的子数组问题 题目说明 数组全为正数,且每个数各不相同,求累加和为K的子数组组合有哪 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- 一加5T刷入魔趣
0.准备工作 1.安装adb工具 2.下载twrp 3.5t系统包. 1.解锁bootloader 先进入原版系统,打开开发者选项,允许USB调试,勾选允许OEM解锁,高级重启选项 打开命令行输入: ...
- 主线程 子线程 调用 ThreadId BeginInvoke调用和Invoke调用
BeginInvoke是异步线程执行 class Program { static int TakeWhile() { int threadid = Thre ...
- yolo v7使用triton部署
https://github.com/WongKinYiu/yolov7/tree/main/deploy/triton-inference-server
- 备份还原ESXi 中的虚拟机快照
了解 ESXi 中的虚拟机快照 (1015180) Click here to view full document Symptoms 免责声明:本文是 Understanding VM snapsh ...
- GFS预报数据下载
#更新#2019年6月12日之后,gfs预报场存放的目录变了,需要修改.get_gfs.pl第51行改为 $URL='https://nomads.ncep.noaa.gov/pub/data/ncc ...
- 《SAP MDM主数据管理》.pdf
<SAP MDM主数据管理>.pdf 有需要的联系 wx :erpworld
- JMETHOD
#include <iostream> #define JMETHOD(type,methodname,arglist) type (*methodname) arglist JMETHO ...
- android 获取手机的唯一id,获取当前时间,获取两位随机数
//获取机器的唯一id public static String getDeviceID() { String deviceID= ""; try{ //一共13位 如果位数不够可 ...
- java第七周学习情况
这个星期 主要是在搞学校在暑期安排的实验报告b 怎么说来着 才知道这个消息几天 这是对学习不上心的体现啊 题目也有点多 慢慢做呗 而Java这边还是看些相关知识呗 说实话 已经学不太懂了 不过没关系 ...
- redhat7
systemctl systemctl enable sshd 开机启动某服务 systemctl disable sshd 开机不启动某服务 systemctl is-enabled sshd查看某 ...