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 ...
随机推荐
- mysql基础篇--库的管理
库的创建 create database [if not exists] 库名; 库的修改 alter database 库名 character set 字符集; #更改库的字符集 库的删除 dro ...
- Django 反向查找(related_name)
先定义两个模型,一个是A,一个是B,是一对多的类型 class A(models.Model): name= models.CharField('名称', max_length=32) class B ...
- VS tools
官方下载,有免费也有试用的 http://visualstudiogallery.msdn.microsoft.com/ VS2012简单的使用感受+插件推荐 http://blog.sina.com ...
- learning armbian steps(5) ----- armbian 构建arm rootfs
基于learning armbian step(4) 的总结,我们来实践一下,接下来的会把整个构建的log都贴出来: vmuser@vmuser-virtual-machine:~/qemu-arm$ ...
- jenkins 邮件发送错误
jenkins 在创建新的 Build 的时候希望邮件进行通知. 但是邮件通知的时候出现错误: Unable to Send Mail - javax.net.ssl.SSLException: Un ...
- maven+SSM+junit+jetty+log4j2环境配置的最佳实践
思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合 pom中的依赖跟着思路一批一批的 ...
- Ubuntu14.04 gzip failed file too large
使用gzip解压一个oracle rman备份集时报错:File too large.gizp -d cosp_db_full.tar.gzgzip: cosp_db_full.tar:File to ...
- Solr 7.X 安装和配置--Linux篇
1. 关闭防火墙和Selinux 2. 安装所需环境JDK 3. 下载Solr7.4版本 4. 下载并配置solr的中文分词器IK Analyzer 5. 启动Solr 6. 注意事项以及说明 1. ...
- linux安装过程中遇到的一些问题总结
后面持续更新 1.安装之后查看显示一直连不上网 vim /etc/sysconfig/network-scripts/ifcfg-eth0 然后应该就可以上网了 2.linux窗口无法适应虚拟机窗口 ...
- Go语言 之捧腹网爬虫案例
package main import ( "fmt" "net/http" "os" "regexp" "s ...