【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 ...
随机推荐
- selenium执行下载多个文件操作,谷歌浏览器弹出"xxx想要下载多个文件"的处理方法
背景: 使用selenium框架,批量下载多个目录的不同文件,而下载多个文件时,浏览器会弹出如下窗口 解决方案有2个:1.代码定位到元素并点击[允许].2.修改浏览器的设置,使其能够拥有自动下载的 ...
- QT管理网络状态和网络连接
参考:http://www.cleartechfei.com/2020/07/qt%E4%BD%BF%E7%94%A8http%E5%8D%8F%E8%AE%AE/ 1. 用Qt框架进行应用开发的过程 ...
- 14-K8S之helm入门到逃跑
目录 helm入门 1.helm介绍 2.helm核心术语 3.helm下载和安装 3.1以helm3.6为测试实例 3.2以helm3.7.2为例 helm v2版本在集群上部署Tiller 1.创 ...
- centos已经安装了python2,同时安装python3
安装相关软件包: yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel ...
- 微信退费报错:SSLHandshakeException No appropriate protocol (protocol is disabled or cipher suites are inappropr)
微信退费报错:No appropriate protocol (protocol is disabled or cipher suites are inappropr) javax.net.ssl.S ...
- 内存参数kernel.shmmax和kernel.shmall的含义
安装oracle数据库的时候,都要设置这个内核参数 vi /etc/sysctl.conf kernel.panic_on_oops = 1 1:程序出错,不继续运行 0:程序出错,继续运行 kern ...
- centos 服务器配置网络ifconfig位置
path : /etc/sysconfig/network-scripts/ifcfg-enoxxx
- android studio 查看工程所有动画资源
- postman或浏览器可以访问,java不能访问的post请求,连接超时
代码中用RestTemplate请求url一直是连接超时 可以修改一下jvm配置 -Djava.net.preferIPv4Stack=true
- vs2019 常用数据结构 纯C语言 头文件实现 (持续更新改错中)单链表,
1.单链表: 1 #pragma once 2 #ifndef _List_H 3 #include<stdio.h> 4 #include<stdlib.h> 5 #defi ...