思路:

对于每个数字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. 小程序setData数据量过大时候会对渲染有影响吗?

    datas:[ { id:1000, name: "帅哥", title: '...', b: '...', d: 0, f:0, .... }, { id:1001, name: ...

  2. 2019牛客多校E Androgynos——自补图&&构造

    题目 给出一个 $n$,判断是否存在 $n$ 个顶点的自补图,如果存在,输出边和映射. 分析 一个无向图若同构于它的补图,则称该图为自补图. 定理:一个自补图一定存在 $4k$ 或 $4k+1$ 个顶 ...

  3. BZOJ 3275: Number (二分图最小割)

    题意 有nnn个数,其中同时满足下面两个条件的数对不能同时选,求选出一些数让和最大. 若两个数aaa,bbb同时满足以下条件,则aaa,bbb不能同时被选 存在正整数ccc,使a∗a+b∗b=c∗ca ...

  4. python自动华 (十)

    Python自动化 [第十篇]:Python进阶-多进程/协程/事件驱动与Select\Poll\Epoll异步IO 本节内容: 多进程 协程 事件驱动与Select\Poll\Epoll异步IO   ...

  5. mouseover([[data],fn])

    mouseover([[data],fn]) 概述 当鼠标指针位于元素上方时,会发生 mouseover 事件. 该事件大多数时候会与 mouseout 事件一起使用.直线电机选型 注释:与 mous ...

  6. javaMail 详解

    原文:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html 一.JavaMail API简介JavaMail API是读取. ...

  7. MySQL区间检索

    在没有前端的情况下,自己写一些搜索逻辑,可能不太完善,不过功能完成了 //区间检索的判定 private String columnTextTranslateRegion(String columnT ...

  8. 又见回文 (SDUT 2560)

    Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.现在呢, ...

  9. 超大地图MMORPG的场景管理

    目前在做一个超大地图MMORPG的场景管理部分,客户端通过动态预读解决了超大图量的动态加载,但是在做人物行走的时候遇到了一些问题: 一张地图上的PLAYER和NPC等是存放在一个list中的,地图超大 ...

  10. 2016"百度之星" - 初赛(Astar Round2A)1005 BD String(HDU5694)——找规律、字符串对称、分治

    分析:按照题目所给的意思每次处理得到的新的字符串都是具有高度对称性的,举个例子,如题目所给的第三个字符串,最中间的是B然后两边分散开去,一边是B的话另外一边关于这个中心对称的那个位置一定是D,反过来同 ...