思路:

对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果。时间复杂度O(n)。

实现:

 class Solution
{
public:
int sumSubarrayMins(vector<int>& A)
{
int res = ;
stack<int> st;
int n = A.size();
const int MOD = 1e9 + ;
for (int i = ; i < n; i++)
{
while (!st.empty() && A[i] < A[st.top()])
{
int tmp = st.top(); st.pop();
int last = st.empty() ? - : st.top();
res = (res + (i - tmp) * (tmp - last) % MOD * A[tmp] % MOD) % MOD;
}
st.push(i);
}
while (!st.empty())
{
int tmp = st.top(); st.pop();
int last = st.empty() ? - : st.top();
res = (res + (n - tmp) * (tmp - last) % MOD * A[tmp] % MOD) % MOD;
}
return res;
}
}

leetcode907 Sum of Subarray Minimums的更多相关文章

  1. [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 ...

  2. 907. Sum of Subarray Minimums

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  3. [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 ...

  4. 子数组最小值的总和 Sum of Subarray Minimums

    2018-09-27 23:33:49 问题描述: 问题求解: 方法一.DP(MLE) 动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划, ...

  5. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  6. LC 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  7. 动态规划-Maximum Subarray-Maximum Sum Circular Subarray

    2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...

  8. [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  9. Maximum Sum Circular Subarray LT918

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

随机推荐

  1. 20、自动装配-@Autowired&@Qualifier&@Primary

    20.自动装配-@Autowired&@Qualifier&@Primary 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值 20.1 @Autowi ...

  2. 时间戳、String、Date转换

    时间戳(String or long) =>Date long s=1557230621043L; Date date=new Date(s); System.out.println(date) ...

  3. tarjan——校园网(缩点,再构图)

    P2746 [USACO5.3]校园网Network of Schools 任务一:求缩完点后入度为0的点的个数(有向边) 任务二:求缩完点后入度为0和出度为0的最大值(要把图构造成强连通分量) 注意 ...

  4. 内存管理2-set方法的内存管理

    1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类  @age @book -------------------- #import "book.h" ...

  5. HTTP header 介绍 转载

    这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...

  6. mxnet快速入门教程

    前段时间工作中用到了MXnet,然而MXnet的文档写的实在是.....所以在这记录点东西,方便自己,也方便大家. 一.MXnet的安装及使用 开源地址:https://github.com/dmlc ...

  7. jboss虚拟机快照

    若使用jboss,则一定要记得隔断时间拍一个快照,这样如果虚拟机坏了,则可以进行回复快照,避免再次花时间去解决问题. 拍快照:启动虚拟机,然后,操作如下. 选择,current State,右击,则可 ...

  8. springmvc返回json对象

    1.引入jackson的依赖 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -- ...

  9. 设置Python打印格式

    >>> def esc(code): ... return f'\033[{code}m' ... >>> print(esc('31;1;4') + 'reall ...

  10. 动态连通性问题——算法union-find

    问题定义:问题的输入是一列整数对,其中每个整数都表示一个某种类型的对象,一对整数p,q可以被理解为"p和q是相连的".我们假设“相连”是一种对等的关系. 这也意味着它具有: 1.自 ...