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 ...
随机推荐
- python自动华 (十二)
Python自动化 [第十二篇]:Python进阶-MySQL和ORM 本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 外键 增 ...
- ArrayBuffer、TypedArray、DataView二进制数组
三个是处理二进制数据的接口.都是类数组. 1.ArrayBuffer是什么? ArrayBuffer是一个二进制对象(文件,图片等).它指向固定长度的,连续的内存区域. const buffer = ...
- @MapperScan和@ComponentScan的区别
区别 今天在撸SpringBoot的时候,突然对注解产生了混淆,@MapperScan和@ComponentScan都是扫描包,二者之间有什么区别呢? 首先,@ComponentScan是组件扫描注解 ...
- F. Make Them Similar ( 暴力折半枚举 + 小技巧 )
传送门 题意: 给你 n 个数 a[ 1 ] ~ a[ n ], n <= 100: 让你找一个 x , 使得 a[ 1 ] = a[ 1 ] ^ x ~ a[ n ] = a[ n ] ^ ...
- [TJOI2019]唱、跳、rap和篮球
嘟嘟嘟 TJ律师函警告 20分暴力比较好拿,因为每一种学生可以理解为无限多,那么总方案数就是\(C_{n} ^ {4}\),然后我们枚举至少讨论cxk的有几组,容斥即可. 需要注意的是,容斥的时候还要 ...
- bus error(总线错误)
转自 http://blog.csdn.net/todd911/article/details/8813321 在<C专家编程>中提到了总线错误bus error(core dumped) ...
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度(SDUT 2804)
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char data ...
- centos7初始化脚本(转)
#!/bin/bash # 描述: CentOS 初始化脚本 # 加载配置文件 if [ -n "${1}" ];then /bin/} fi # 可接受配置(shell 变量格式 ...
- Po类设计
0.承接MySQL 表设计,同样地,这篇博客中一部分内容是Deolin的个人观点和习惯. 1.一般Po类的域是和DB表字段一一对应的, 而由于每个信息表和关联表都有id.insert_time.upd ...
- Python 不覆盖输入txt 读取txt
不覆盖输入: with open('1.txt','rt')as f: data=f.read() print(data+"\n") 读取txt: with open('1.txt ...