leetcode907 Sum of Subarray Minimums
思路:
对于每个数字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的更多相关文章
- [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 ...
- 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] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- 子数组最小值的总和 Sum of Subarray Minimums
2018-09-27 23:33:49 问题描述: 问题求解: 方法一.DP(MLE) 动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划, ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 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 ...
- 动态规划-Maximum Subarray-Maximum Sum Circular Subarray
2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...
- [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 ...
- Maximum Sum Circular Subarray LT918
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
随机推荐
- BZOJ 1299: [LLH邀请赛]巧克力棒 【SG函数/博弈分析/高斯消元】
因为太懒,放个博客 我只写了O(2n)O(2^n)O(2n)的 CODE #include <cstdio> int n, x[15]; int main () { for(int T = ...
- UVA323 Jury Compromise
思路:背包类DP 提交:3次 错因:没有注意得分的上下界导致 RE 显示 WA 题解: 我们很容易的想到把两种分数做一个差,来尽量背到 \(0\) . 那最大化总分呢?这时我们可以用两种分数的和作为物 ...
- Lock的lock/unlock, condition的await/singal 和 Object的wait/notify 的区别
在使用Lock之前,我们都使用Object 的wait和notify实现同步的.举例来说,一个producer和consumer,consumer发现没有东西了,等待,produer生成东西了,唤醒. ...
- Oracle 后台进程(二)DBWR进程
一.DBWR进程介绍 DBWR进程执行将数据块缓冲区写入数据文件的工作,是负责缓冲存储管理的一个Oracle后台进程.在修改DB Cache中的某个缓冲区时,会将它标志为“DIRTY”,DBWR的主要 ...
- springboot之搭建第一个helloworld程序
1.下载基本框架 在网站:https://start.spring.io/ 全部默认,基本没有改动 选择依赖,当然也可以自己在pom.xml加,我们直接在这里选择. 只选择Spring Web Sta ...
- 编写第一个Servlet程序
在开始这一节之前呢,我们还需要把Tomcat配置到Eclipse中,配置的方式很简单,打开Eclipse,Window,Preferences,进入到这个页面 将Tomcat的安装目录配置到Eclip ...
- 'builtin_function_or_method' object has no attribute 'translate'
'builtin_function_or_method' object has no attribute 'translate' 首先这个错误的意思是:内建函数或方法对象不能转换成对应的属性. #错误 ...
- linux系统rwx(421)、777权限详解
摘要 linux的常见权限,mark一下 常用的linux文件权限如下: 444 r--r--r-- 600 rw------- 644 rw-r--r-- 666 rw-rw-rw- 700 rwx ...
- tmux 入门踩坑记录
软件安装 sudo apt-get install tmux 1. 分割左右窗口 ^b -> % 运行 tmux 新建一个 tmux 的会话(session),此时窗口唯一的变化是在底部会出现一 ...
- Ajax提交之后,Method从POST变成GET
https://developer.aliyun.com/ask/68268?spm=a2c6h.13159736 https://blog.csdn.net/uzizi/article/detail ...